Files
2025-10-25 03:02:53 +03:00

5.9 KiB
Raw Permalink Blame History

[optional.monadic]

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

1

#

Let U be invoke_result_t<F, decltype(*val)>.

2

#

Mandates: remove_cvref_t is a specialization of optional.

3

#

Effects: Equivalent to:if (*this) {return invoke(std::forward(f), *val);} else {return remove_cvref_t();}

🔗

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

4

#

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

5

#

Mandates: remove_cvref_t is a specialization of optional.

6

#

Effects: Equivalent to:if (*this) {return invoke(std::forward(f), std::move(*val));} else {return remove_cvref_t();}

🔗

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

7

#

Let U be remove_cv_t<invoke_result_t<F, decltype(*val)>>.

8

#

Mandates: U is a valid contained type for optional.

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

[Note 1:

There is no requirement that U is movable ([dcl.init.general]).

— end note]

9

#

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

🔗

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

10

#

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

11

#

Mandates: U is a valid contained type for optional.

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

[Note 2:

There is no requirement that U is movable ([dcl.init.general]).

— end note]

12

#

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

🔗

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

13

#

Constraints: F models invocable andT models copy_constructible.

14

#

Mandates: is_same_v<remove_cvref_t<invoke_result_t>, optional> is true.

15

#

Effects: Equivalent to:if (*this) {return *this;} else {return std::forward(f)();}

🔗

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

16

#

Constraints: F models invocable andT models move_constructible.

17

#

Mandates: is_same_v<remove_cvref_t<invoke_result_t>, optional> is true.

18

#

Effects: Equivalent to:if (*this) {return std::move(*this);} else {return std::forward(f)();}