std::expected<T,E>::transform
来自cppreference.com
| 主模板 |
||
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 对象,以 f 的结果初始化该值(或在结果类型是 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)));
参数
| f | - | 适合的函数或 可调用 (Callable) 对象,它的返回类型不是引用类型 |
返回值
给定表达式 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()))
| |
示例
| 本节未完成 原因:暂无示例 |
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 3938 | C++23 | 通过 value() 获取预期值[1] | 改成 **this |
| LWG 3973 | C++23 | 通过 **this 获取预期值[2] | 改成 val
|
参阅
若含有预期值则返回 expected 本身,否则返回含有变换后非预期值的 expected (公开成员函数) |