命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | utility‎ | expected
 
 
 
 
主模板
template< class F >
constexpr auto transform_error( F&& f ) &;
(1) (自 C++23 起)
template< class F >
constexpr auto transform_error( F&& f ) const&;
(2) (自 C++23 起)
template< class F >
constexpr auto transform_error( F&& f ) &&;
(3) (自 C++23 起)
template< class F >
constexpr auto transform_error( F&& f ) const&&;
(4) (自 C++23 起)
void 部分特化
template< class F >
constexpr auto transform_error( F&& f ) &;
(5) (自 C++23 起)
template< class F >
constexpr auto transform_error( F&& f ) const&;
(6) (自 C++23 起)
template< class F >
constexpr auto transform_error( F&& f ) &&;
(7) (自 C++23 起)
template< class F >
constexpr auto transform_error( F&& f ) const&&;
(8) (自 C++23 起)

如果 *this 包含一个意外值,则以 f*this 的意外值作为参数调用 f,并返回一个包含意外值的 std::expected 对象,该意外值使用 f 的结果初始化。否则,返回一个表示期望值的 std::expected 对象。

1-4) 期望值使用 *this 的期望值 val 初始化。

给定类型 G

1,2) std::remove_cv_t<std::invoke_result_t<F, decltype(error())>>
3,4) std::remove_cv_t<std::invoke_result_t<F, decltype(std::move(error()))>>
5,6) std::remove_cv_t<std::invoke_result_t<F, decltype(error())>>
7,8) std::remove_cv_t<std::invoke_result_t<F, decltype(std::move(error()))>>

如果满足以下任何条件,则程序是非良构的

  • G 不是 std::unexpected 的有效模板实参。
  • 以下对应的声明是非良构的
1,2) G g(std::invoke(std::forward<F>(f), error()));
3,4) G g(std::invoke(std::forward<F>(f), std::move(error()));
5,6) G g(std::invoke(std::forward<F>(f), error()));
7,8) G g(std::invoke(std::forward<F>(f), std::move(error()));


1,2) 仅当 std::is_constructible_v<T, decltype((val))>true 时,此重载参与重载决议。
3,4) 仅当 std::is_constructible_v<T, decltype(std::move(val))>true 时,此重载参与重载决议。

目录

[编辑] 参数

f - 一个合适的函数或 可调用 对象,其调用签名返回非引用类型

[编辑] 返回值

给定表达式 expr

1,2) std::invoke(std::forward<F>(f), error())
3,4) std::invoke(std::forward<F>(f), std::move(error()))
5,6) std::invoke(std::forward<F>(f), error())
7,8) std::invoke(std::forward<F>(f), std::move(error()))

返回值定义如下

 重载  has_value() 的值
true false
(1,2) std::expected<T, G>(std::in_place, val) std::expected<T, G>
    (std::unexpect, expr)
(3,4) std::expected<T, G>(std::in_place, std::move(val))
(5,6) std::expected<T, G>()
(7,8)

[编辑] 示例

[编辑] 缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。

DR 应用于 已发布行为 正确行为
LWG 3938 C++23 期望值通过 value()[1] 获得 更改为 **this
LWG 3973 C++23 期望值通过 **this[2] 获得 更改为 val
  1. value() 要求 E 是可复制构造的(参见 LWG issue 3843),而 operator* 则不然。
  2. **this 可能触发 实参依赖查找

[编辑] 参见

如果 expected 自身包含一个期望值,则返回它;否则,返回给定函数在意外值上的结果
(公共成员函数) [编辑]
如果 expected 存在,则返回包含转换后的期望值的 expected;否则,返回 expected 自身
(公共成员函数) [编辑]