Namespaces
Variants

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

From cppreference.net
Utilities library
プライマリテンプレート
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 を呼び出し、その結果(結果の型が void の場合は値初期化された)で初期化された期待値を持つ std::expected オブジェクトを返す。それ以外の場合、 * this の非期待値で初期化された非期待値を持つ std::expected オブジェクトを返す。

1-4) f は、 val の期待値を持つ * this を引数として呼び出されます。
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 であり、以下の対応する宣言は不適格です:
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 ) )
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 ( ) ) )

不具合報告

以下の動作変更の欠陥報告書は、以前に公開された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 argument-dependent lookup を引き起こす可能性がある。

関連項目

期待される値が含まれている場合は expected 自体を返す。それ以外の場合は、変換された予期しない値を含む expected を返す
(公開メンバ関数)