[optional.monadic] (original) (raw)

22 General utilities library [utilities]

22.5 Optional objects [optional]

22.5.3 Class template optional [optional.optional]

22.5.3.8 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(*val)>.

Mandates: remove_cvref_t<U> is a specialization of optional.

Effects: Equivalent to:if (*this) { return invoke(std::forward<F>(f), *val);} 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(*val))>.

Mandates: remove_cvref_t<U> is a specialization of optional.

Effects: Equivalent to:if (*this) { return invoke(std::forward<F>(f), std::move(*val));} 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(*val)>>.

Mandates: U is a non-array object type other than in_place_t or nullopt_t.

The declarationU u(invoke(std::forward<F>(f), *val));is well-formed for some invented variable u.

Returns: If *this contains a value, an optional<U> object whose contained value is direct-non-list-initialized withinvoke(std​::​forward<F>(f), *val); otherwise, optional<U>().

template<class F> constexpr auto transform(F&& f) &&;template<class F> constexpr auto transform(F&& f) const &&;

Let U beremove_cv_t<invoke_result_t<F, decltype(std​::​move(*val))>>.

Mandates: U is a non-array object type other than in_place_t or nullopt_t.

The declarationU u(invoke(std::forward<F>(f), std::move(*val)));is well-formed for some invented variable u.

Returns: If *this contains a value, an optional<U> object whose contained value is direct-non-list-initialized withinvoke(std​::​forward<F>(f), std​::​move(*val)); otherwise, optional<U>().

template<class F> constexpr optional or_else(F&& f) const &;

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) &&;

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)();}