# Poco

# template < typename C >

# class Optional

Library: Foundation
Package: Core
Header: Poco/Optional.h

## Description

Optional is a simple wrapper class for value types that allows to introduce a specified/unspecified state to value objects.

An Optional can be default constructed. In this case, the Optional will have a Null value and isSpecified() will return false. Calling value()(without default value) on a Null object will throw a NullValueException.

An Optional can also be constructed from a value. It is possible to assign a value to an Optional, and to reset an Optional to contain a Null value by calling clear().

For use with Optional, the value type should support default construction.

Note that the Optional class is basically the same as Nullable. However, serializers may treat Nullable and Optional differently. An example is XML serialization based on XML Schema, where Optional would be used for an element with minOccurs == 0, whereas Nullable would be used on an element with nillable == true.

## Member Summary

Member Functions: assign, clear, isSpecified, operator =, swap, value

## Constructors

### Optional

Optional() = default;

Creates an empty Optional.

### Optional

Optional(
const C & value
);

Creates a Optional with the given value.

### Optional

Optional(
C && value
);

Creates a Optional by moving the given value.

### Optional

Optional(
const Optional & other
);

Creates a Optional by copying another one.

### Optional

Optional(
Optional && other
) noexcept;

Creates a Optional by moving another one.

## Destructor

### ~Optional

~Optional() = default;

Destroys the Optional.

## Member Functions

### assign

Optional & assign(
const C & value
);

Assigns a value to the Optional.

### assign

Optional & assign(
C && value
);

Moves a value into the Optional.

### assign

Optional & assign(
const Optional & other
);

Assigns another Optional.

### clear

void clear();

Clears the Optional.

### isSpecified

bool isSpecified() const;

Returns true iff the Optional's value has been specified.

### operator =

Optional & operator = (
const C & value
);

### operator =

Optional & operator = (
C && value
);

### operator =

Optional & operator = (
const Optional & other
);

### operator =

Optional & operator = (
Optional && other
) noexcept;

### swap

void swap(
Optional & other
) noexcept;

### value

const C & value() const;

Returns the Optional's value.

Throws a Poco::NullValueException if the value has not been specified.

### value

const C & value(
const C & deflt
) const;

Returns the Optional's value, or the given default value if the Optional's value has not been specified.