std::expected<T,E>::and_then
来自 cppreference.com
主模板 |
||
template< class F > constexpr auto and_then( F&& f ) &; |
(1) | (自 C++23 起) |
template< class F > constexpr auto and_then( F&& f ) const&; |
(2) | (自 C++23 起) |
template< class F > constexpr auto and_then( F&& f ) &&; |
(3) | (自 C++23 起) |
template< class F > constexpr auto and_then( F&& f ) const&&; |
(4) | (自 C++23 起) |
void 部分特化 |
||
template< class F > constexpr auto and_then( F&& f ) &; |
(5) | (自 C++23 起) |
template< class F > constexpr auto and_then( F&& f ) const&; |
(6) | (自 C++23 起) |
template< class F > constexpr auto and_then( F&& f ) &&; |
(7) | (自 C++23 起) |
template< class F > constexpr auto and_then( F&& f ) const&&; |
(8) | (自 C++23 起) |
如果 *this 代表一个预期的值,则调用 f 并返回其结果。否则,返回一个包含意外值的 std::expected
对象,该对象使用 *this 的意外值初始化。
5-8) f 在不带任何参数的情况下被调用。
给定类型 U
作为
5-8) std::remove_cvref_t<std::invoke_result_t<F>>
如果 U
不是 std::expected
的特化,或者 std::is_same_v<U::error_type, E> 为 false,则程序格式错误。
内容 |
[编辑] 参数
f | - | 一个合适的函数或 可调用 对象,返回一个 std::expected |
[编辑] 返回值
重载 | has_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())) |
[edit] Notes
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_expected |
202211L | (C++23) | Monadic functions for std::expected |
[edit] Example
This section is incomplete Reason: no example |
[edit] Defect reports
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 by value()[1] | changed to **this |
LWG 3973 | C++23 | the expected value was obtained by **this[2] | changed to val |
- ↑
value()
requiresE
to be copy constructible (see LWG issue 3843), whereoperator*
does not. - ↑ **this can trigger argument-dependent lookup.
[edit] See also
(C++23) |
in-place construction tag for unexpected value in expected (tag) |
returns an expected containing the transformed expected value if it exists; otherwise, returns the expected itself(public member function) |