名前空間
変種
操作

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

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 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` が期待値(expected value)を表す場合、`f` を呼び出し、その結果を返します。そうでない場合、`*this` の予期しない値(unexpected value)で初期化された `std::expected` オブジェクトを返します。

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` が `false` の場合、プログラムは ill-formed となります。

1,2) これらのオーバーロードは、`std::is_constructible_v` が `true` の場合にのみオーバーロード解決に参加します。
3,4) これらのオーバーロードは、`std::is_constructible_v` が `true` の場合にのみオーバーロード解決に参加します。
5,6) これらのオーバーロードは、`std::is_constructible_v` が `true` の場合にのみオーバーロード解決に参加します。
7,8) これらのオーバーロードは、`std::is_constructible_v` が `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` は 引数依存名前探索 をトリガーする可能性があります。

[編集] 関連項目

expected 内の期待されない値を直接構築するためのタグ
(tag)[編集]
期待される値が存在する場合、変換された期待される値を含む expected を返し、そうでなければ expected 自体を返す
(public member function) [編集]
English 日本語 中文(简体) 中文(繁體)