CVB++ 14.0
optional< T > Class Template Referencefinal

This class is a replacement for C++17 std::optional. More...

#include <cvb/shims/optional.hpp>

Public Member Functions

 optional () noexcept
 Default ctor not storing a value.
 
 optional (nullopt_t) noexcept
 Ctor for explicitly not storing a value.
 
template<class... ARGS>
 optional (in_place_t, ARGS &&... args)
 In-place constructs a new T object. More...
 
template<class U , typename std::enable_if< std::is_assignable< T &, U >::value, int >::type = 0>
 optional (U &&value)
 Ctor from an object assignable to T. More...
 
template<class U , typename std::enable_if< std::is_assignable< T &, U >::value, int >::type = 0>
optionaloperator= (U &&value)
 Assignment operator for an object assignable to T. More...
 
template<typename std::enable_if< std::is_copy_constructible< T >::value, int >::type = 0>
 optional (const optional &rhs)
 Copy ctor. More...
 
template<typename std::enable_if< std::is_copy_assignable< T >::value, int >::type = 0>
optionaloperator= (const optional &rhs)
 Copy operator. More...
 
template<typename std::enable_if< std::is_move_constructible< T >::value, int >::type = 0>
 optional (optional &&rhs)
 Move ctor. More...
 
template<typename std::enable_if< std::is_move_assignable< T >::value, int >::type = 0>
optionaloperator= (optional &&rhs)
 Move operator. More...
 
 ~optional ()
 Dtor.
 
const T * operator-> () const noexcept
 Gets the reference to the stored object. More...
 
T * operator-> () noexcept
 Gets the reference to the stored object. More...
 
const T & operator* () const noexcept
 Gets the reference to the stored object. More...
 
T & operator* () noexcept
 Gets the reference to the stored object. More...
 
 operator bool () const noexcept
 Gets whether this optional contains a value. More...
 
template<class... ARGS>
T & emplace (ARGS &&... args)
 Constructs a new T in-place. More...
 
bool has_value () const noexcept
 Gets whether this optional contains a value. More...
 
void reset ()
 Destroys any contained value.
 
void swap (optional &other)
 Swaps this object with the other object. More...
 
const T & value () const
 Gets the contained value. More...
 
T & value ()
 Gets the contained value. More...
 
template<class U , typename std::enable_if< std::is_assignable< T &, U >::value, int >::type = 0>
value_or (U &&default_value) const
 Gets the contained value if has_value() is true; default_value otherwise. More...
 

Related Functions

(Note that these are not member functions.)

template<class T >
optional< typename std::decay< T >::type > make_optional (T &&value)
 Factory function to create an optional from a T object. More...
 
template<class T , class... ARGS>
optional< T > make_optional (ARGS &&... args)
 Factory function to in-place construct a T in an optional. More...
 

Detailed Description

template<class T>
class Cvb::Shims::optional< T >

This class is a replacement for C++17 std::optional.

Constructor & Destructor Documentation

◆ optional() [1/4]

optional ( in_place_t  ,
ARGS &&...  args 
)
inline

In-place constructs a new T object.

Template Parameters
ARGSType list of all arguments to be forwarded to T.
Parameters
[in]argsArguments that are forwarded to the ctor of

◆ optional() [2/4]

optional ( U &&  value)
inline

Ctor from an object assignable to T.

Template Parameters
UType assignable to T.
Parameters
[in]valueValue to store.

◆ optional() [3/4]

optional ( const optional< T > &  rhs)
inline

Copy ctor.

Parameters
[in]rhsObject to copy.

◆ optional() [4/4]

optional ( optional< T > &&  rhs)
inline

Move ctor.

Parameters
[in]rhsObject to copy.

Member Function Documentation

◆ emplace()

T & emplace ( ARGS &&...  args)
inline

Constructs a new T in-place.

If a this object has_value() is true, the old value is destroyed via its destructor.

Template Parameters
ARGSType list of all arguments to be forwarded to T.
Parameters
[in]argsCommand line arguments forwarded to the ctor of T.
Returns
Reference to the newly created object.

◆ has_value()

bool has_value ( ) const
inlinenoexcept

Gets whether this optional contains a value.

Returns
true if this object contains a value;

◆ operator bool()

operator bool ( ) const
inlineexplicitnoexcept

Gets whether this optional contains a value.

Returns
true if this object contains a value;

◆ operator*() [1/2]

const T & operator* ( ) const
inlinenoexcept

Gets the reference to the stored object.

Precondition
has_value() == true
Returns
Reference to the contained value.

◆ operator*() [2/2]

T & operator* ( )
inlinenoexcept

Gets the reference to the stored object.

Precondition
has_value() == true
Returns
Reference to the contained value.

◆ operator->() [1/2]

const T * operator-> ( ) const
inlinenoexcept

Gets the reference to the stored object.

Precondition
has_value() == true
Returns
Reference to the contained value.

◆ operator->() [2/2]

T * operator-> ( )
inlinenoexcept

Gets the reference to the stored object.

Precondition
has_value() == true
Returns
Reference to the contained value.

◆ operator=() [1/3]

optional & operator= ( const optional< T > &  rhs)
inline

Copy operator.

Parameters
[in]rhsObject to copy.
Returns
Reference to this object.

◆ operator=() [2/3]

optional & operator= ( optional< T > &&  rhs)
inline

Move operator.

Parameters
[in]rhsObject to copy.
Returns
Reference to this object.

◆ operator=() [3/3]

optional & operator= ( U &&  value)
inline

Assignment operator for an object assignable to T.

Template Parameters
UType assignable to T.
Parameters
[in]valueValue to store.
Returns
Reference to this object.

◆ swap()

void swap ( optional< T > &  other)
inline

Swaps this object with the other object.

The contents of this and the other are swapped.

Parameters
[in]otherOptional to swap value with.

◆ value() [1/2]

T & value ( )
inline

Gets the contained value.

Returns
Reference to the contained value.
Exceptions
bad_optional_accessthrown if has_value() is false.

◆ value() [2/2]

const T & value ( ) const
inline

Gets the contained value.

Returns
Reference to the contained value.
Exceptions
bad_optional_accessthrown if has_value() is false.

◆ value_or()

T value_or ( U &&  default_value) const
inline

Gets the contained value if has_value() is true; default_value otherwise.

Returns
Either the contained value or the default_value.

Friends And Related Function Documentation

◆ make_optional() [1/2]

optional< T > make_optional ( ARGS &&...  args)
related

Factory function to in-place construct a T in an optional.

Template Parameters
TType of value to store.
ARGSType list of all arguments to be forwarded to T.
Parameters
[in]argsArguments to be forwarded to the ctor of T.
Returns
optional with newly constructed T.

◆ make_optional() [2/2]

optional< typename std::decay< T >::type > make_optional ( T &&  value)
related

Factory function to create an optional from a T object.

Template Parameters
TType of value to store.
Parameters
[in]valueValue to be forwarded to the optional.
Returns
optional with the set value.