名前空間
変種
操作

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

From cppreference.com
 
 
ユーティリティライブラリ
言語サポート
型のサポート (基本型、RTTI)
ライブラリ機能検査マクロ (C++20)
プログラムユーティリティ
可変引数関数
コルーチンサポート (C++20)
契約サポート (C++26)
三方比較
(C++20)
(C++20)(C++20)(C++20)  
(C++20)(C++20)(C++20)

汎用ユーティリティ
関係演算子 (C++20で非推奨)
 
 
プライマリテンプレート
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がexpected値である場合、fを呼び出し、その結果を含むexpected値を持つ`std::expected`オブジェクトを返します(結果の型がvoidの場合は値初期化されます)。それ以外の場合、`*this`のunexpected値を含むunexpected値を持つ`std::expected`オブジェクトを返します。

1-4) fは、expected値 val を引数として呼び出されます。
5-8) fは引数なしで呼び出されます。

Uという型は以下のように定義されます

1,2) std::remove_cv_t<std::invoke_result_t<F, decltype((val))>>
3,4) std::remove_cv_t<std::invoke_result_t<F, decltype(std::move(val))>>

以下のいずれかの条件が満たされる場合、プログラムは不適格です。

  • Uは`std::expected`の有効な値型ではありません。
  • std::is_void_v<U>falseであり、以下の対応する宣言がill-formedとなります。
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)));


1,2) これらのオーバーロードは、std::is_constructible_v<E, decltype(error())>trueである場合にのみ、オーバーロード解決に参加します。
3,4) これらのオーバーロードは、std::is_constructible_v<E, decltype(std::move(error()))>trueである場合にのみ、オーバーロード解決に参加します。
5,6) これらのオーバーロードは、std::is_constructible_v<E, decltype(error())>trueである場合にのみ、オーバーロード解決に参加します。
7,8) これらのオーバーロードは、std::is_constructible_v<E, decltype(std::move(error()))>trueである場合にのみ、オーバーロード解決に参加します。

目次

[編集] パラメータ

f - 非参照型の値を返す、適切な関数または Callable オブジェクト

[編集] 戻り値

expr は以下のように定義されます。

1,2) std::invoke(std::forward<F>(f), val)
3,4) std::invoke(std::forward<F>(f),std::move(val))

戻り値は以下のように定義されます。

 オーバーロード  `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()))

[編集]

[編集] 不具合報告

以下の動作変更を伴う欠陥報告が、以前に公開された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 を返す
(public member function) [編集]
English 日本語 中文(简体) 中文(繁體)