Public Member Functions | Related Functions | List of all members
aurora::Optional< T > Class Template Reference

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.
 
Optionaloperator= (T object)
 Assign from T object (move or copy)
 
Optionaloperator= (const Optional &origin)
 Copy assignment operator.
 
Optionaloperator= (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...
 

Detailed Description

template<typename T>
class aurora::Optional< T >

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:

This class offers a new way:

The syntax is very similar to a pointer, with dereferencing operators and conversion to bool contexts:

aurora::Optional<T> opt = find();
if (opt) // check if 'opt' has a value
use(*opt); // dereference 'opt' to access the T object

You can even simplify this code further, using type inference and the little-known C++ feature of declarations inside if statements:

if (auto opt = find())
use(*opt);

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.

Member Function Documentation

template<typename T>
T& aurora::Optional< T >::operator* ( )

Returns the contained object.

Warning
Dereferencing empty optionals yields undefined behavior.
template<typename T>
const T& aurora::Optional< T >::operator* ( ) const

Returns the contained object (const overload).

Warning
Dereferencing empty optionals yields undefined behavior.
template<typename T>
T* aurora::Optional< T >::operator-> ( )

Returns the contained object for member access.

Warning
Dereferencing empty optionals yields undefined behavior.
template<typename T>
const T* aurora::Optional< T >::operator-> ( ) const

Returns the contained object for member access (const overload).

Warning
Dereferencing empty optionals yields undefined behavior.

The documentation for this class was generated from the following file: