Template Class optional

Class Documentation

template<class T>
class optional

This class template manages an optional contained value, i.e. a value that may or may not be present.

Public Types

using type = T

Public Functions

optional() = default

Default constructor.

inline optional(const T &val) noexcept

Copy constructor from an instance of the templated class.

inline optional(T &&val) noexcept

Move constructor from an instance of the templated class.

inline optional(const optional<T> &val) noexcept

Copy constructor.

inline optional(optional<T> &&val) noexcept

Move constructor.

~optional() = default

Destructor.

template<class ...Args>
inline void emplace(Args&&... _args)

Constructs the contained value in-place.

Parameters:

_args[in] The arguments to pass to the constructor.

inline void reset(bool initial_engaged = false)

Reset the state of the optional.

Parameters:

initial_engaged[in] True value initializes the state with a default instance of the templated class. False value leaves the optional in a uninitialized state.

inline T &value() &

Returns the contained value.

Throws:

exception::BadOptionalAccessException – This exception is thrown when the optional is uninitialized.

Returns:

The contained value.

inline const T &value() const &

Returns the contained value.

Throws:

exception::BadOptionalAccessException – This exception is thrown when the optional is uninitialized.

Returns:

The contained value.

inline T &&value() &&

Returns the contained value.

Throws:

exception::BadOptionalAccessException – This exception is thrown when the optional is uninitialized.

Returns:

The contained value.

inline const T &&value() const &&

Returns the contained value.

Throws:

exception::BadOptionalAccessException – This exception is thrown when the optional is uninitialized.

Returns:

The contained value.

inline bool has_value() const

Checks whether the optional contains a value.

Returns:

Whether the optional contains a value.

inline optional &operator=(const optional &opt)

Assigns content from an optional.

inline optional &operator=(optional &&opt)

Assigns content from an optional.

inline optional &operator=(const T &val)

Assigns content from an instance of the templated class.

inline optional &operator=(T &&val)

Assigns content from an instance of the templated class.

inline optional &operator=(nullopt_t) noexcept

Uninitialized the optional.

inline bool operator==(const optional &opt_val) const

Compares optional values.

inline bool operator!=(const optional &opt_val) const

Compares optional values.

inline T &operator*() & noexcept

Accesses the contained value.

The behavior is undefined if *this does not contain a value.

Returns:

The contained value.

inline const T &operator*() const & noexcept

Accesses the contained value.

The behavior is undefined if *this does not contain a value.

Returns:

The contained value.

inline T &&operator*() && noexcept

Accesses the contained value.

The behavior is undefined if *this does not contain a value.

Returns:

The contained value.

inline const T &&operator*() const && noexcept

Accesses the contained value.

The behavior is undefined if *this does not contain a value.

Returns:

The contained value.

inline T *operator->() noexcept

Accesses the contained value.

The behavior is undefined if *this does not contain a value.

Returns:

The contained value.

inline const T *operator->() const noexcept

Accesses the contained value.

The behavior is undefined if *this does not contain a value.

Returns:

The contained value.

inline explicit operator bool() const noexcept

Checks whether the optional contains a value.