Namespaces
Variants

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

From cppreference.net
Utilities library
メインテンプレート
template < class F >
constexpr auto and_then ( F && f ) & ;
(1) (C++23以降)
template < class F >
constexpr auto and_then ( F && f ) const & ;
(2) (C++23以降)
template < class F >
constexpr auto and_then ( F && f ) && ;
(3) (C++23以降)
template < class F >
constexpr auto and_then ( F && f ) const && ;
(4) (C++23以降)
void 部分特殊化
template < class F >
constexpr auto and_then ( F && f ) & ;
(5) (C++23以降)
template < class F >
constexpr auto and_then ( F && f ) const & ;
(6) (C++23以降)
template < class F >
constexpr auto and_then ( F && f ) && ;
(7) (C++23以降)
template < class F >
constexpr auto and_then ( F && f ) const && ;
(8) (C++23以降)

* this が期待値を表す場合、 f を呼び出し、その結果を返します。それ以外の場合、 std::expected オブジェクトを返します。このオブジェクトは、 * this の非期待値で初期化された非期待値を含みます。

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

U が以下のように与えられた場合:

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

U std::expected の特殊化でない場合、または std:: is_same_v < U :: error_type , E > false の場合、プログラムは不適格です。

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 オブジェクトで、 std::expected を返すもの

戻り値

オーバーロード has_value() の値
true false
( 1,2 ) std:: invoke ( std:: forward < F > ( f ) , val ) U ( std:: unexpect , error ( ) )
( 3,4 ) std:: invoke ( std:: forward < F > ( f ) ,std :: move ( val ) ) U ( std:: unexpect , std :: move ( error ( ) ) )
( 5,6 ) std:: invoke ( std:: forward < F > ( f ) ) U ( std:: unexpect , error ( ) )
( 7,8 ) U ( 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 に変更された
  1. value() E がコピー構築可能であることを要求する( LWG issue 3843 を参照)。一方で operator* はこの要求を持たない。
  2. ** this argument-dependent lookup を引き起こす可能性がある。

関連項目

expected 内の予期しない値に対するインプレース構築タグ
(タグ)
期待される値が存在する場合は変換された期待値を含む expected を返す。それ以外の場合は expected 自体を返す
(公開メンバ関数)