|
Q_DECL_CONSTEXPR | Optional () |
| Constructs an empty Optional object.
|
|
Q_DECL_CONSTEXPR | Optional (const T &value) |
| Constructs an non-empty Optional object and from the value.
|
|
template<typename U > |
Q_DECL_CONSTEXPR | Optional (const Optional< U > &other) |
| Constructs a new Optional object that is a copy of the given other object. More...
|
|
Optional & | operator= (const T &value) |
| Assigns contents to the value and makes this object non-empty.
|
|
T & | value () |
| Returns the reference to the contained value. More...
|
|
const T & | value () const |
| Returns the const reference to the contained value. More...
|
|
template<typename U > |
T | value (const U &defaultValue) const |
| Returns the contained value if object is not empty or defaultValue otherwise.
|
|
void | swap (Optional &other) |
| Swaps this Optional object with the other.
|
|
void | reset () |
| Makes this Optional object empty.
|
|
T & | operator* () |
| Returns a reference to the contained value. More...
|
|
const T & | operator* () const |
| Returns a const reference to the contained value. More...
|
|
T * | operator-> () |
| Returns a pointer to the contained value. More...
|
|
const T * | operator-> () const |
| Returns a const pointer to the contained value. More...
|
|
template<typename T>
class Optional< T >
The Optional class manages an optional contained value, i.e.
a value that semantically may not be present.
A common use case for optional is the return value of a function that may fail. As opposed to other approaches, such as QPair<T, bool>, optional is more readable, as the intent is expressed explicitly.
The value is guaranteed to be allocated within the optional object itself, i.e. no dynamic memory allocation ever takes place. Thus, an optional object models an object, not a pointer, even though the operator*() and operator->() are defined.
Optinal can be in 2 states - empty or not. The optional object is not empty on the following conditions:
- The object is initialized with a value of type T
- The object is assigned an non-empty optional. The object is empty on the following conditions:
- The object is default-initialized.
- The object is initialized with a value of NullOptional or an empty optional object.
- The object is assigned a value of NullOptional or an empty optional.