From cppreference.com
Primary template
template<classF>constexprautoor_else(F&&f)&; (1)(since C++23)template<classF>constexprautoor_else(F&&f)const&; (2)(since C++23)template<classF>constexprautoor_else(F&&f)&&; (3)(since C++23)template<classF>constexprautoor_else(F&&f)const&&; (4)(since C++23)void partial specialization
template<classF>constexprautoor_else(F&&f)&; (5)(since C++23)template<classF>constexprautoor_else(F&&f)const&; (6)(since C++23)template<classF>constexprautoor_else(F&&f)&&; (7)(since C++23)template<classF>constexprautoor_else(F&&f)const&&; (8)(since C++23)If *this contains an unexpected value, invokes f with the unexpected value of *this as the argument and returns its result. Otherwise, returns a std::expected object that represents an expected value.
1-4) The expected value is initialized with the expected value
of *this.
Given type G as:
1,2)std::remove_cvref_t<std::invoke_result_t<F,decltype(error())>>
3,4)std::remove_cvref_t<std::invoke_result_t<F,decltype(std::move(error()))>>
5,6)std::remove_cvref_t<std::invoke_result_t<F,decltype(error())>>
7,8)std::remove_cvref_t<std::invoke_result_t<F,decltype(std::move(error()))>>
If G is not a specialization of std::expected, or std::is_same_v<G::value_type,T> is false, the program is ill-formed.
1,2) These overloads participate in overload resolution only if std::is_constructible_v<T,decltype((val))> is true.
3,4) These overloads participate in overload resolution only if std::is_constructible_v<T,decltype(std::move(val))> is true.
Parameters
f - a suitable function or
object that returns a std::expectedReturn value
Overload Value of
truefalse(
)G(std::in_place,val)std::invoke(std::forward<F>(f),error())(
)G(std::in_place,std::move(val))std::invoke(std::forward<F>(f),std::move(error()))(
)G()std::invoke(std::forward<F>(f),error())(
)std::invoke(std::forward<F>(f),std::move(error()))Notes
macro ValueStdFeature
(C++23)Monadic functions for std::expectedExample
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
C++23 the expected value was obtained by value()
changed to **this
C++23 the expected value was obtained by **this
changed to
requires E to be copy constructible (see
), where
does not.
**this can trigger
.
See also
returns the expected itself if it contains an expected value; otherwise, returns an expected containing the transformed unexpected value
(public member function)