std::expected<T,E>::transform
来自 cppreference.cn
主模板 |
||
template< class F > constexpr auto transform( F&& f ) &; |
(1) | (C++23 起) |
template< class F > constexpr auto transform( F&& f ) const&; |
(2) | (C++23 起) |
template< class F > constexpr auto transform( F&& f ) &&; |
(3) | (C++23 起) |
template< class F > constexpr auto transform( F&& f ) const&&; |
(4) | (C++23 起) |
void 偏特化 |
||
template< class F > constexpr auto transform( F&& f ) &; |
(5) | (C++23 起) |
template< class F > constexpr auto transform( F&& f ) const&; |
(6) | (C++23 起) |
template< class F > constexpr auto transform( F&& f ) &&; |
(7) | (C++23 起) |
template< class F > constexpr auto transform( F&& f ) const&&; |
(8) | (C++23 起) |
如果 *this 表示一个期望值,则调用 f 并返回一个 std::expected
对象,该对象包含一个期望值,并用其结果初始化(如果结果类型为 void,则进行值初始化)。否则,返回一个 std::expected
对象,该对象包含一个非期望值,并用 *this 的非期望值初始化。
5-8) f 不带任何参数调用。
给定类型 U
为
5-8) std::remove_cv_t<std::invoke_result_t<F>>
如果满足以下任何条件,程序将不正确:
-
U
不是std::expected
的有效值类型。 - std::is_void_v<U> 为 false,并且以下对应的声明格式错误
1,2) U u(std::invoke(std::forward<F>(f),
val
));3,4) U u(std::invoke(std::forward<F>(f), std::move(
val
)));5-8) U u(std::invoke(std::forward<F>(f)));
目录 |
[edit] 参数
f | - | 一个合适的函数或可调用 (Callable) 对象,其调用签名返回一个非引用类型 |
[edit] 返回值
给定表达式 expr 为
1,2) std::invoke(std::forward<F>(f),
val
)3,4) std::invoke(std::forward<F>(f),std::move(
val
))5-8) std::invoke(std::forward<F>(f))
返回值定义如下:
重载 | has_value() 的值 | |
---|---|---|
true | false | |
(1,2) |
|
std::expected<U, E>(std::unexpect, error()) |
(3,4) | std::expected<U, E> (std::unexpect, std::move(error()))
| |
(5,6) | std::expected<U, E>(std::unexpect, error()) | |
(7,8) | std::expected<U, E> (std::unexpect, std::move(error()))
|
[edit] 示例
本节不完整 原因:无示例 |
[edit] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 3938 | C++23 | 期望值通过 value()[1] 获得 | 更改为 **this |
LWG 3973 | C++23 | 期望值通过 **this[2] 获得 | 更改为 val |
[edit] 另请参阅
如果 expected 包含预期值,返回 expected 自身;否则返回包含转换后的非预期值的 expected (公共成员函数) |