Image Formats NG
Public Member Functions | Related Functions | List of all members
Optional< T > Class Template Reference

The Optional class manages an optional contained value, i.e. More...

Public Member Functions

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...
 
Optionaloperator= (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 >
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...
 

Related Functions

(Note that these are not member functions.)

bool operator== (const Optional< T > &left, const Optional< T > &right)
 
bool operator< (const Optional< T > &left, const Optional< T > &right)
 
bool operator> (const Optional< T > &left, const Optional< T > &right)
 
bool operator== (const Optional< T > &optional, const T &value)
 
bool operator== (const T &value, const Optional< T > &optional)
 
bool operator< (const Optional< T > &optional, const T &value)
 
bool operator> (const Optional< T > &optional, const T &value)
 
bool operator< (const T &value, const Optional< T > &optional)
 
bool operator> (const T &value, const Optional< T > &optional)
 

Detailed Description

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:

Constructor & Destructor Documentation

◆ Optional()

template<typename T>
template<typename U >
Optional< T >::Optional ( const Optional< U > &  other)
inline

Constructs a new Optional object that is a copy of the given other object.

This constructor tries to implicitly convert value from U type to T.

Member Function Documentation

◆ operator*() [1/2]

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

Returns a reference to the contained value.

Note
This function asserts if this object is empty.

◆ operator*() [2/2]

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

Returns a const reference to the contained value.

Note
This function asserts if this object is empty.

◆ operator->() [1/2]

template<typename T>
T * Optional< T >::operator-> ( )
inline

Returns a pointer to the contained value.

Note
This function asserts if this object is empty.

◆ operator->() [2/2]

template<typename T>
const T * Optional< T >::operator-> ( ) const
inline

Returns a const pointer to the contained value.

Note
This function asserts if this object is empty.

◆ value() [1/2]

template<typename T>
T & Optional< T >::value ( )
inline

Returns the reference to the contained value.

Note
This function asserts if this object is empty.

◆ value() [2/2]

template<typename T>
const T & Optional< T >::value ( ) const
inline

Returns the const reference to the contained value.

Note
This function asserts if this object is empty.

Friends And Related Function Documentation

◆ operator<() [1/3]

template<typename T>
bool operator< ( const Optional< T > &  left,
const Optional< T > &  right 
)
related

Returns true if the left Optional object is less than the right object.

Note
Empty Optional is less than non-empty Optional.

◆ operator<() [2/3]

template<typename T>
bool operator< ( const Optional< T > &  optional,
const T &  value 
)
related

Returns true if the optional's value is less than the value.

Note
Empty Optional is always less than any value.

◆ operator<() [3/3]

template<typename T>
bool operator< ( const T &  value,
const Optional< T > &  optional 
)
related

Returns true if the value is less than the optional's value.

Note
Empty Optional is always less than any value.

◆ operator==() [1/3]

template<typename T>
bool operator== ( const Optional< T > &  left,
const Optional< T > &  right 
)
related

Returns true if the left Optional object is equal to the right object.

◆ operator==() [2/3]

template<typename T>
bool operator== ( const Optional< T > &  optional,
const T &  value 
)
related

Returns true if the optional contains the value.

◆ operator==() [3/3]

template<typename T>
bool operator== ( const T &  value,
const Optional< T > &  optional 
)
related

Returns true if the optional contains the value.

◆ operator>() [1/3]

template<typename T>
bool operator> ( const Optional< T > &  left,
const Optional< T > &  right 
)
related

Returns true if the left Optional object is greater than the right object.

Note
Non-empty Optional is greater than an empty Optional.

◆ operator>() [2/3]

template<typename T>
bool operator> ( const Optional< T > &  optional,
const T &  value 
)
related

Returns true if the optional's value is greater than the value.

Note
Any value is always greater than an empty Optional.

◆ operator>() [3/3]

template<typename T>
bool operator> ( const T &  value,
const Optional< T > &  optional 
)
related

Returns true if the value is greater than the optional's value.

Note
Any value is always greater than an empty Optional.