Namespaces
Variants

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

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

汎用ユーティリティ
関係演算子 (C++20 で非推奨)
整数比較関数
(C++20)(C++20)(C++20)    
(C++20)
Swap および 型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
共通語彙型
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
 
主テンプレート
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 の unexpected 値で初期化された unexpected 値を保持する *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))>>
5-8) std::remove_cvref_t<std::invoke_result_t<F>>

もし U が std::expected の特殊化でない場合、または std::is_same_v<U::error_type, E> が false である場合、プログラムは ill-formed です。

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自体を返す
(公開メンバ関数)