Namespaces
Variants

std::expected<T,E>:: transform

From cppreference.net
Utilities library
主模板
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 的非期望值初始化。

1-4) f * this 的期望值 val 作为参数进行调用。
5-8) f 在未提供任何参数的情况下被调用。

给定类型 U 为:

1,2) std:: remove_cv_t < std:: invoke_result_t < F, decltype ( ( val ) ) >>
3,4) std:: remove_cv_t < std:: invoke_result_t < F, decltype ( std :: move ( val ) ) >>

若满足以下任一条件,则程序非良构:

  • 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 ) ) ) ;


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 对象,其调用签名返回非引用类型

返回值

给定表达式 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
  1. value() 要求 E 可复制构造(参见 LWG 问题 3843 ),而 operator* 无此要求。
  2. ** this 可能触发 实参依赖查找

参见

若当前 expected 包含期望值则返回其自身;否则返回包含转换后非期望值的 expected
(公开成员函数)