|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Main template | ||
template<class F> constexprauto and_then( F&& f)&; | (1) | (since C++23) |
template<class F> constexprauto and_then( F&& f)const&; | (2) | (since C++23) |
template<class F> constexprauto and_then( F&& f)&&; | (3) | (since C++23) |
template<class F> constexprauto and_then( F&& f)const&&; | (4) | (since C++23) |
void partial specialization | ||
template<class F> constexprauto and_then( F&& f)&; | (5) | (since C++23) |
template<class F> constexprauto and_then( F&& f)const&; | (6) | (since C++23) |
template<class F> constexprauto and_then( F&& f)&&; | (7) | (since C++23) |
template<class F> constexprauto and_then( F&& f)const&&; | (8) | (since C++23) |
If*this represents an expected value, invokesf and returns its result. Otherwise, returns astd::expected object that contains an unexpected value, which is initialized with the unexpected value of*this.
Given typeU as:
IfU is not a specialization ofstd::expected, orstd::is_same_v<U::error_type, E> isfalse, the program is ill-formed.
Contents |
| f | - | a suitable function orCallable object that returns astd::expected |
| Overload | Value ofhas_value() | |
|---|---|---|
| true | false | |
| (1,2) | std::invoke(std::forward<F>(f), val) | U(std::unexpect, error()) |
| (3,4) | std::invoke(std::forward<F>(f),std::move(val)) | U(std::unexpect, std::move(error())) |
| (5,6) | std::invoke(std::forward<F>(f)) | U(std::unexpect, error()) |
| (7,8) | U(std::unexpect, std::move(error())) | |
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_expected | 202211L | (C++23) | Monadic functions forstd::expected |
| This section is incomplete Reason: no example |
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 3938 | C++23 | the expected value was obtained byvalue()[1] | changed to**this |
| LWG 3973 | C++23 | the expected value was obtained by**this[2] | changed toval |
value() requiresE to be copy constructible (seeLWG issue 3843), whereoperator* does not.(C++23) | in-place construction tag for unexpected value inexpected(tag)[edit] |
returns anexpected containing the transformed expected value if it exists; otherwise, returns theexpected itself(public member function)[edit] |