std::expected<T,E>::transform
ja.cppreference.net より
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オブジェクトを返します。そうでなければ、std::expectedのコピーを含むerror()オブジェクトを返します。
Tが(cv 修飾されていないかもしれない)voidでない場合、保持されている値 (operator*()) がfの引数として渡されます。そうでなければ、fは引数を取りません。
ここでUを次のように定義します:
Tが(cv 修飾されていないかもしれない)voidでない場合:- オーバーロード (1-2)については、
std::remove_cv_t<std::invoke_result_t<F, decltype(operator*())>>; - オーバーロード (3-4)については、
std::remove_cv_t<std::invoke_result_t<F, decltype(std::move(operator*()))>>;
- オーバーロード (1-2)については、
- それ以外の場合 (
Tは可能性として cv 修飾されたvoidである)、std::remove_cv_t<std::invoke_result_t<F>>.
Uはstd::expectedの有効な値型でなければなりません。U型の変数は呼び出しの結果から構築可能でなければなりません(ただし、ムーブ構築可能である必要はありません)。戻り値の型はstd::expected<U, E>です。
1-2) これらのオーバーロードは、
std::is_constructible_v<E, decltype(error())>がtrueである場合にのみオーバーロード解決に参加します。3-4) これらのオーバーロードは、
std::is_constructible_v<E, decltype(std::move(error()))>がtrueである場合にのみオーバーロード解決に参加します。形式的には、これらの関数は以下のステップを実行します:
*thisが期待値valを保持している場合:
fを次のように呼び出します:std::invoke(std::forward<F>(f), val)オーバーロード (1,2) で、std::is_void_v<T>がfalseの場合;std::invoke(std::forward<F>(f), std::move(val))オーバーロード (3,4) で、std::is_void_v<T>がfalseの場合;std::invoke(std::forward<F>(f))std::is_void_v<T>がtrueの場合。
- その後:
std::is_void_v<U>がfalseであれば、期待値を含むstd::expectedオブジェクトを、呼び出し結果から直接初期化して返します;- そうでなければ、
std::expected<U, E>()を返します。
- それ以外の場合 (
*thisがエラー値を保持している)、std::expected<U, E>(std::unexpect, error())を返します。
パラメータ
| 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
(
)
)
)
|
|
注釈
| 機能テストマクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_expected |
202211L |
(C++23) | 単子関数(std::expected
|
例
|
このセクションは不完全です
理由: 例がありません |
不具合報告
以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
| LWG 3938 | C++23 | 期待値は value ( ) [1] によって取得されていた | ** this に変更 |
| LWG 3973 | C++23 | 期待値は ** this [2] によって取得されていた |
val
に変更
|
-
↑
value()はEがコピー構築可能であることを要求する( LWG issue 3843 を参照)。一方operator*はこの要求を持たない。 - ↑ ** this は argument-dependent lookup を引き起こす可能性がある。
関連項目
期待される値が含まれている場合は
expected
自体を返す。それ以外の場合は、変換された予期しない値を含む
expected
を返す
(公開メンバ関数) |