Represents optional values. More...
Public Member Functions | |
Optional () | |
Construct empty optional with default constructor. | |
Optional (NulloptType) | |
Construct empty optional from aurora::nullopt. | |
template<typename... Args> | |
Optional (InplaceType, Args &&...args) | |
Construct object in-place, forwarding arguments. | |
Optional (T object) | |
Construct implicitly from T object (move or copy) | |
Optional (const Optional &origin) | |
Copy constructor. | |
Optional (Optional &&source) | |
Move constructor. | |
Optional & | operator= (T object) |
Assign from T object (move or copy) | |
Optional & | operator= (const Optional &origin) |
Copy assignment operator. | |
Optional & | operator= (Optional &&source) |
Move assignment operator. | |
~Optional () | |
Destructor. | |
void | swap (Optional &other) |
Swaps two optional objects. | |
T & | operator* () |
Returns the contained object. More... | |
const T & | operator* () const |
Returns the contained object (const overload). More... | |
T * | operator-> () |
Returns the contained object for member access. More... | |
const T * | operator-> () const |
Returns the contained object for member access (const overload). More... | |
operator SafeBool () const | |
Check if the optional object contains a value. | |
Related Functions | |
(Note that these are not member functions.) | |
template<typename T > | |
void | swap (Optional< T > &lhs, Optional< T > &rhs) |
Swaps two optionals. | |
template<typename T , typename... Args> | |
Optional< T > | makeOptional (Args &&...args) |
Emplaces an object directly inside the aurora::Optional object. More... | |
template<typename T > | |
bool | operator== (const Optional< T > &lhs, const Optional< T > &rhs) |
Equality comparison. More... | |
template<typename T > | |
bool | operator!= (const Optional< T > &lhs, const Optional< T > &rhs) |
Inequality comparison. More... | |
Represents optional values.
This class extends objects with an additional null state, which explicitly denotes the absence of a valid object. There are many situations where an object can either carry a valid state or none, especially in function return values. For example, let's assume we have a function find()
that searches for a T object, but doesn't always find one. C++ offers many ways to deal with this situation, but the usual ones come with certain drawbacks:
T* find()
– the pointer must refer to a stored object (no temporary) or require dynamic allocationiterator find()
+ comparison with end()
– requires an underlying container and exposes implementation detailsbool has()
+ T find()
– requires two calls, thus two possibly expensive searches instead of onebool find(T& out)
– user must construct T up-front, requiring a default constructor, incurring useless initialization and preventing const qualifierThis class offers a new way:
Optional<T> find()
The syntax is very similar to a pointer, with dereferencing operators and conversion to bool contexts:
You can even simplify this code further, using type inference and the little-known C++ feature of declarations inside if statements:
This class can be used as an interim solution until std::optional
becomes part of a C++ standard and is supported by all major compilers. aurora::Optional uses a very similar (although slightly smaller) API.
T& aurora::Optional< T >::operator* | ( | ) |
Returns the contained object.
const T& aurora::Optional< T >::operator* | ( | ) | const |
Returns the contained object (const overload).
T* aurora::Optional< T >::operator-> | ( | ) |
Returns the contained object for member access.
const T* aurora::Optional< T >::operator-> | ( | ) | const |
Returns the contained object for member access (const overload).