std::expected<T,E>::and_then
来自 zh.cppreference.net
| 主模板 |
||
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 为:
1,2)
std::remove_cvref_t<std::invoke_result_t<F, decltype((val))>>3,4)
std::remove_cvref_t<std::invoke_result_t<F, decltype(std::move(val))>>5-8)
std::remove_cvref_t<std::invoke_result_t<F>>如果 U 不是 std::expected 的特化,或者 std::is_same_v<U::error_type, E> 是 false,则程序非良构。
1,2) 这些重载仅在
std::is_constructible_v<E, decltype(error())> 是 true 时参与重载决议。3,4) 这些重载仅在
std::is_constructible_v<E, decltype(std::move(error()))> 是 true 时参与重载决议。5,6) 这些重载仅在
std::is_constructible_v<E, decltype(error())> 是 true 时参与重载决议。7,8) 这些重载仅在
std::is_constructible_v<E, decltype(std::move(error()))> 是 true 时参与重载决议。参数
| f | - | 一个合适的函数或 Callable 对象,其返回值为 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 ( ) ) ) | |
注释
| 功能测试 宏 | 值 | 标准 | 功能 |
|---|---|---|---|
__cpp_lib_expected
|
202211L
|
(C++23) |
std::expected
的单子函数
|
示例
|
本节内容不完整
原因:缺少示例 |
缺陷报告
下列行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
| 缺陷报告 | 适用范围 | 发布时行为 | 修正后行为 |
|---|---|---|---|
| LWG 3938 | C++23 | 期望值通过 value ( ) [1] 获取 | 改为 ** this |
| LWG 3973 | C++23 | 期望值通过 ** this [2] 获取 |
改为
val
|
参见
|
(C++23)
|
用于在
expected
中就地构造意外值的标签
(标签) |
若期望值存在则返回包含转换后期望值的
expected
;否则返回
expected
本身
(公开成员函数) |