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
物件,該物件包含一個期望值,並用其結果初始化(如果結果型別為 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)));
目錄 |
[edit] 引數
f | - | 一個合適的函式或可呼叫 (Callable) 物件,其呼叫簽名返回一個非引用型別 |
[edit] 返回值
給定表示式 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()))
|
[edit] 示例
本節不完整 原因:無示例 |
[edit] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 3938 | C++23 | 期望值透過 value()[1] 獲得 | 更改為 **this |
LWG 3973 | C++23 | 期望值透過 **this[2] 獲得 | 更改為 val |
[edit] 另請參閱
如果 expected 包含預期值,返回 expected 自身;否則返回包含轉換後的非預期值的 expected (公共成員函式) |