Template Class optional

Inheritance Relationships

Base Types

Class Documentation

template<class T>
class optional : private tl::detail::optional_move_assign_base<T>, private tl::detail::optional_delete_ctor_base<T>, private tl::detail::optional_delete_assign_base<T>

An optional object is an object that contains the storage for another object and manages the lifetime of this contained object, if any. The contained object may be initialized after the optional object has been initialized, and may be destroyed before the optional object has been destroyed. The initialization state of the contained object is tracked by the optional object.

Public Types

using value_type = T

Public Functions

template<class F> inline TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t< F, T & > and_then (F &&f) &

Carries out some operation which returns an optional on the stored object if there is one.

template<class F> inline TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t< F, T && > and_then (F &&f) &&
template<class F>
inline constexpr detail::invoke_result_t<F, const T&> and_then(F &&f) const &
template<class F>
inline constexpr detail::invoke_result_t<F, const T&&> and_then(F &&f) const &&
template<class F, detail::enable_if_ret_void< F > * = nullptr> inline optional< T > TL_OPTIONAL_11_CONSTEXPR or_else (F &&f) &

Calls f if the optional is empty.

template<class F, detail::disable_if_ret_void< F > * = nullptr> inline optional< T > TL_OPTIONAL_11_CONSTEXPR or_else (F &&f) &
template<class F, detail::enable_if_ret_void<F>* = nullptr>
inline optional<T> or_else(F &&f) &&
template<class F, detail::disable_if_ret_void< F > * = nullptr> inline optional< T > TL_OPTIONAL_11_CONSTEXPR or_else (F &&f) &&
template<class F, detail::enable_if_ret_void<F>* = nullptr>
inline optional<T> or_else(F &&f) const &
template<class F, detail::disable_if_ret_void< F > * = nullptr> inline optional< T > TL_OPTIONAL_11_CONSTEXPR or_else (F &&f) const &
template<class F, detail::enable_if_ret_void<F>* = nullptr>
inline optional<T> or_else(F &&f) const &&
template<class F, detail::disable_if_ret_void<F>* = nullptr>
inline optional<T> or_else(F &&f) const &&
template<class F, class U>
inline U map_or(F &&f, U &&u) &

Maps the stored value with f if there is one, otherwise returns u.

template<class F, class U>
inline U map_or(F &&f, U &&u) &&
template<class F, class U>
inline U map_or(F &&f, U &&u) const &
template<class F, class U>
inline U map_or(F &&f, U &&u) const &&
template<class F, class U>
inline detail::invoke_result_t<U> map_or_else(F &&f, U &&u) &

Maps the stored value with f if there is one, otherwise calls u and returns the result.

template<class F, class U>
inline detail::invoke_result_t<U> map_or_else(F &&f, U &&u) &&
template<class F, class U>
inline detail::invoke_result_t<U> map_or_else(F &&f, U &&u) const &
template<class F, class U>
inline detail::invoke_result_t<U> map_or_else(F &&f, U &&u) const &&
template<class U>
inline constexpr optional<typename std::decay<U>::type> conjunction(U &&u) const

Returns u if *this has a value, otherwise an empty optional.

inline TL_OPTIONAL_11_CONSTEXPR optional disjunction (const optional &rhs) &

Returns rhs if *this is empty, otherwise the current value.

inline constexpr optional disjunction(const optional &rhs) const &
inline TL_OPTIONAL_11_CONSTEXPR optional disjunction (const optional &rhs) &&
inline constexpr optional disjunction(const optional &rhs) const &&
inline TL_OPTIONAL_11_CONSTEXPR optional disjunction (optional &&rhs) &
inline constexpr optional disjunction(optional &&rhs) const &
inline TL_OPTIONAL_11_CONSTEXPR optional disjunction (optional &&rhs) &&
inline constexpr optional disjunction(optional &&rhs) const &&
inline optional take()

Takes the value out of the optional, leaving it empty.

constexpr optional() noexcept = default

Constructs an optional that does not contain a value.

inline constexpr optional(nullopt_t) noexcept
TL_OPTIONAL_11_CONSTEXPR optional(const optional &rhs) = default

Copy constructor

If rhs contains a value, the stored value is direct-initialized with it. Otherwise, the constructed optional is empty.

TL_OPTIONAL_11_CONSTEXPR optional(optional &&rhs) = default

Move constructor

If rhs contains a value, the stored value is direct-initialized with it. Otherwise, the constructed optional is empty.

template<class ...Args>
inline explicit constexpr optional(detail::enable_if_t<std::is_constructible<T, Args...>::value, in_place_t>, Args&&... args)

Constructs the stored value in-place using the given arguments.

template<class U, class ...Args>
inline explicit TL_OPTIONAL_11_CONSTEXPR optional(detail::enable_if_t<std::is_constructible<T, std::initializer_list<U>&, Args&&...>::value, in_place_t>, std::initializer_list<U> il, Args&&... args)
template<class U = T, detail::enable_if_t<std::is_convertible<U&&, T>::value>* = nullptr, detail::enable_forward_value<T, U>* = nullptr>
inline constexpr optional(U &&u)

Constructs the stored value with u.

template<class U = T, detail::enable_if_t<!std::is_convertible<U&&, T>::value>* = nullptr, detail::enable_forward_value<T, U>* = nullptr>
inline explicit constexpr optional(U &&u)
template<class U, detail::enable_from_other<T, U, const U&>* = nullptr, detail::enable_if_t<std::is_convertible<const U&, T>::value>* = nullptr>
inline optional(const optional<U> &rhs)

Converting copy constructor.

template<class U, detail::enable_from_other<T, U, const U&>* = nullptr, detail::enable_if_t<!std::is_convertible<const U&, T>::value>* = nullptr>
inline explicit optional(const optional<U> &rhs)
template<class U, detail::enable_from_other<T, U, U&&>* = nullptr, detail::enable_if_t<std::is_convertible<U&&, T>::value>* = nullptr>
inline optional(optional<U> &&rhs)

Converting move constructor.

template<class U, detail::enable_from_other<T, U, U&&>* = nullptr, detail::enable_if_t<!std::is_convertible<U&&, T>::value>* = nullptr>
inline explicit optional(optional<U> &&rhs)
~optional() = default

Destroys the stored value if there is one.

inline optional &operator=(nullopt_t) noexcept

Assignment to empty.

Destroys the current value if there is one.

optional &operator=(const optional &rhs) = default

Copy assignment.

Copies the value from rhs if there is one. Otherwise resets the stored value in *this.

optional &operator=(optional &&rhs) = default

Move assignment.

Moves the value from rhs if there is one. Otherwise resets the stored value in *this.

template<class U = T, detail::enable_assign_forward<T, U>* = nullptr>
inline optional &operator=(U &&u)

Assigns the stored value from u, destroying the old value if there was one.

template<class U, detail::enable_assign_from_other<T, U, const U&>* = nullptr>
inline optional &operator=(const optional<U> &rhs)

Converting copy assignment operator.

Copies the value from rhs if there is one. Otherwise resets the stored value in *this.

template<class U, detail::enable_assign_from_other<T, U, U>* = nullptr>
inline optional &operator=(optional<U> &&rhs)

Converting move assignment operator.

Moves the value from rhs if there is one. Otherwise resets the stored value in *this.

template<class ...Args>
inline T &emplace(Args&&... args)

Constructs the value in-place, destroying the current one if there is one.

template<class U, class ...Args>
inline detail::enable_if_t<std::is_constructible<T, std::initializer_list<U>&, Args&&...>::value, T&> emplace(std::initializer_list<U> il, Args&&... args)
inline void swap(optional &rhs) noexcept(std::is_nothrow_move_constructible<T>::value && detail::is_nothrow_swappable<T>::value)

Swaps this optional with the other.

If neither optionals have a value, nothing happens. If both have a value, the values are swapped. If one has a value, it is moved to the other and the movee is left valueless.

inline constexpr const T *operator->() const

Returns a pointer to the stored value.

inline TL_OPTIONAL_11_CONSTEXPR T * operator-> ()
inline TL_OPTIONAL_11_CONSTEXPR T & operator* () &

Returns the stored value.

inline constexpr const T &operator*() const &
inline TL_OPTIONAL_11_CONSTEXPR T && operator* () &&
inline constexpr const T &&operator*() const &&
inline constexpr bool has_value() const noexcept

Returns whether or not the optional has a value.

inline explicit constexpr operator bool() const noexcept
inline TL_OPTIONAL_11_CONSTEXPR T & value () &

Returns the contained value if there is one, otherwise throws bad_optional_access

inline TL_OPTIONAL_11_CONSTEXPR const T & value () const &
inline TL_OPTIONAL_11_CONSTEXPR T && value () &&
inline TL_OPTIONAL_11_CONSTEXPR const T && value () const &&
template<class U>
inline constexpr T value_or(U &&u) const &

Returns the stored value if there is one, otherwise returns u

template<class U> inline TL_OPTIONAL_11_CONSTEXPR T value_or (U &&u) &&
inline void reset() noexcept

Destroys the stored value if one exists, making the optional empty.

Public Members

template<class F> decltype(optional_map_impl(std::declval< optional & >(), std::declval< F && >())) TL_OPTIONAL_11_CONSTEXPR map (F &&f) &

Carries out some operation on the stored object if there is one.

template<class F> decltype(optional_map_impl(std::declval< optional && >(), std::declval< F && >())) TL_OPTIONAL_11_CONSTEXPR map (F &&f) &&
template<class F>
constexpr decltype(optional_map_impl(std::declval<const optional&>(), std::declval<F&&>())) map(F &&f) const &
template<class F>
constexpr decltype(optional_map_impl(std::declval<const optional&&>(), std::declval<F&&>())) map(F &&f) const &&
template<class F> decltype(optional_map_impl(std::declval< optional & >(), std::declval< F && >())) TL_OPTIONAL_11_CONSTEXPR transform (F &&f) &

Carries out some operation on the stored object if there is one.

template<class F> decltype(optional_map_impl(std::declval< optional && >(), std::declval< F && >())) TL_OPTIONAL_11_CONSTEXPR transform (F &&f) &&
template<class F>
constexpr decltype(optional_map_impl(std::declval<const optional&>(), std::declval<F&&>())) transform(F &&f) const &
template<class F>
constexpr decltype(optional_map_impl(std::declval<const optional&&>(), std::declval<F&&>())) transform(F &&f) const &&