std::expected<T,E>::transform_error
来自cppreference.com
<tbody>
</tbody>
| 主模板 |
||
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 包含非预期值,那么以 *this 的非预期值作为实参调用 f 并返回一个包含预期值的 std::expected 对象,以 f 的结果初始化该值。否则返回一个表示预期值的 std::expected 对象。
给定类型 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 | - | 适合的函数或可调用 (Callable) 对象,它的返回类型不是引用类型 |
返回值
给定表达式 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++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 3938 | C++23 | 通过 value() 获取预期值[1]
|
改成 **this
|
| LWG 3973 | C++23 | 通过 **this 获取预期值[2]
|
改成 val
|
参阅
若 expected 含有预期值则返回其自身,否则返回给定的函数在非预期值上的结果 (公开成员函数) | |
若存在预期值则返回含有变换后的预期值的 expected,否则返回 expected 本身 (公开成员函数) |