std::expected<T,E>::and_then
From cppreference.com
| メインテンプレート |
||
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` が次のように与えられます。
5-8) std::remove_cvref_t<std::invoke_result_t<F>>
`U` が `std::expected` の特殊化ではない場合、または `std::is_same_v
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` に変更されました。 |
- ↑ `value()` は `E` がコピー構築可能であることを要求します(LWG issue 3843 を参照)。`operator*` はそうではありません。
- ↑ `*this` は 引数依存名前探索 をトリガーする可能性があります。
[編集] 関連項目
| (C++23) |
expected 内の期待されない値を直接構築するためのタグ(tag) |
期待される値が存在する場合、変換された期待される値を含む expected を返し、そうでなければ expected 自体を返す(public member function) |