20 General utilities library [utilities]

20.6 Optional objects [optional]

20.6.3 Class template optional [optional.optional]

20.6.3.7 Monadic operations [optional.monadic]

template<class F> constexpr auto and_then(F&& f) &; template<class F> constexpr auto and_then(F&& f) const&;
Let U be invoke_­result_­t<F, decltype(value())>.
Mandates: remove_­cvref_­t<U> is a specialization of optional.
Effects: Equivalent to: if (*this) { return invoke(std::forward<F>(f), value()); } else { return remove_cvref_t<U>(); }
template<class F> constexpr auto and_then(F&& f) &&; template<class F> constexpr auto and_then(F&& f) const&&;
Let U be invoke_­result_­t<F, decltype(std​::​move(value()))>.
Mandates: remove_­cvref_­t<U> is a specialization of optional.
Effects: Equivalent to: if (*this) { return invoke(std::forward<F>(f), std::move(value())); } else { return remove_cvref_t<U>(); }
template<class F> constexpr auto transform(F&& f) &; template<class F> constexpr auto transform(F&& f) const&;
Let U be remove_­cv_­t<invoke_­result_­t<F, decltype(value())>>.
Mandates: U is a non-array object type other than in_­place_­t or nullopt_­t.
The declaration U u(invoke(std::forward<F>(f), value())); is well-formed for some invented variable u.
[Note 1:
There is no requirement that U is movable ([dcl.init.general]).
— end note]
Returns: If *this contains a value, an optional<U> object whose contained value is direct-non-list-initialized with invoke(std​::​forward<F>(f), value()); otherwise, optional<U>().
template<class F> constexpr auto transform(F&& f) &&; template<class F> constexpr auto transform(F&& f) const&&;
Let U be remove_­cv_­t<invoke_­result_­t<F, decltype(std​::​move(value()))>>.
Mandates: U is a non-array object type other than in_­place_­t or nullopt_­t.
The declaration U u(invoke(std::forward<F>(f), std::move(value()))); is well-formed for some invented variable u.
[Note 2:
There is no requirement that U is movable ([dcl.init.general]).
— end note]
Returns: If *this contains a value, an optional<U> object whose contained value is direct-non-list-initialized with invoke(std​::​forward<F>(f), std​::​move(value())); otherwise, optional<U>().
template<class F> constexpr optional or_else(F&& f) const&;
Constraints: F models invocable<> and T models copy_­constructible.
Mandates: is_­same_­v<remove_­cvref_­t<invoke_­result_­t<F>>, optional> is true.
Effects: Equivalent to: if (*this) { return *this; } else { return std::forward<F>(f)(); }
template<class F> constexpr optional or_else(F&& f) &&;
Constraints: F models invocable<> and T models move_­constructible.
Mandates: is_­same_­v<remove_­cvref_­t<invoke_­result_­t<F>>, optional> is true.
Effects: Equivalent to: if (*this) { return std::move(*this); } else { return std::forward<F>(f)(); }