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

来自cppreference.com
< cpp‎ | utility‎ | expected
 
 
工具库
通用工具
日期和时间
函数对象
格式化库 (C++20)
(C++11)
关系运算符 (C++20 中弃用)
整数比较函数
(C++20)(C++20)(C++20)
(C++20)
swap 与类型运算
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
常用词汇类型
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
初等字符串转换
(C++17)
(C++17)
 
 
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 起)

如果 *this 包含期待的值,调用 f 并返回一个包含了其结果的 std::expected 对象;否则,返回一个包含了 error() 的复制的 std::expected 对象。

如果 T 不是(可有 cv 限定的) void ,包含的期待的值( **this )将作为参数传递给 f ;否则 f 不接受任何参数。

令 U 为:

  • 如果 T 不是(可有 cv 限定的) void :
    • 对于重载 (1-2) , std::remove_cv_t<std::invoke_result_t<F, decltype(**this)>> ;
    • 对于重载 (3-4) , std::remove_cv_t<std::invoke_result_t<F, decltype(std::move(**this))>> ;
  • 否则 ( T 是(可有 cv 限定的) void ), std::remove_cv_t<std::invoke_result_t<F>> 。

U 必须是 std::expected 合法的期待的值类型。具有 U 类型的变量必须能从调用结果构造(但不一定需要是可移动构造)。返回值类型为 std::expected<U, E> 。

1-2) 如果 *this 包含期待的值:
  1. 如同通过以下方式调用 f :
  2. 然后
    • 如果 std::is_void_v<U> 为 false ,返回一个包含期待的值的 std::expected 对象,所含值从调用的结果 直接初始化 ;
    • 否则,返回 std::expected<U, E>() 。

否则( *this 包含不期待的值),返回 std::expected<U, E>(std::unexpect, error()) 。

这些重载只有在std::is_constructible_v<E, decltype(error())> 为 true 时才会参与重载决议.
3-4) 如果 *this 包含期待的值:
  1. 如同通过以下方式调用 f :
  2. 然后
    • 如果 std::is_void_v<U> 为 false ,返回一个包含期待的值的 std::expected 对象,所含值从调用的结果 直接初始化 ;
    • 否则,返回 std::expected<U, E>() 。

否则( *this 包含不期待的值),返回 std::expected<U, E>(std::unexpect, std::move(error())) 。

这些重载只有在std::is_constructible_v<E, decltype(std::move(error()))> 为 true 时才会参与重载决议.

参数

f - 适合的函数或 可调用 (Callable) 对象,其返回类型为非引用类型

返回值

一个包含了 f 的结果或不期待的值的 std::expected 对象,如上所述

示例

参阅

若含有期待的值则返回 expected 本身,否则返回含有变换后不期待的值的 expected
(公开成员函数)