名前空間
変種
操作

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

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 or_else( F&& f ) &;
(1) (C++23から)
template< class F >
constexpr auto or_else( F&& f ) const&;
(2) (C++23から)
template< class F >
constexpr auto or_else( F&& f ) &&;
(3) (C++23から)
template< class F >
constexpr auto or_else( F&& f ) const&&;
(4) (C++23から)
void 部分特殊化
template< class F >
constexpr auto or_else( F&& f ) &;
(5) (C++23から)
template< class F >
constexpr auto or_else( F&& f ) const&;
(6) (C++23から)
template< class F >
constexpr auto or_else( F&& f ) &&;
(7) (C++23から)
template< class F >
constexpr auto or_else( F&& f ) const&&;
(8) (C++23から)

現在のオブジェクトが例外値を含んでいる場合、例外値である `*this` を引数として `f` を呼び出し、その結果を返します。それ以外の場合は、期待される値を表す `std::expected` オブジェクトを返します。

1-4) 期待される値は、`*this` の期待される値である `val` で初期化されます。

与えられた型 `G` は次のようになります。

1,2) std::remove_cvref_t<std::invoke_result_t<F, decltype(error())>>
3,4) std::remove_cvref_t<std::invoke_result_t<F, decltype(std::move(error()))>>
5,6) std::remove_cvref_t<std::invoke_result_t<F, decltype(error())>>
7,8) std::remove_cvref_t<std::invoke_result_t<F, decltype(std::move(error()))>>

もし `G` が `std::expected` の特殊化ではない場合、または `std::is_same_v` が `false` である場合、プログラムは不正形式となります。

1,2) これらのオーバーロードは、`std::is_constructible_v` が `true` の場合にのみオーバーロード解決に参加します。
3,4) これらのオーバーロードは、`std::is_constructible_v` が `true` の場合にのみオーバーロード解決に参加します。

目次

[編集] パラメータ

f - `std::expected` を返す適切な関数または Callable オブジェクト。

[編集] 戻り値

 オーバーロード  `has_value()` の値。
true false
(1,2) G(std::in_place, val) std::invoke(std::forward<F>(f), error())
(3,4) G(std::in_place, std::move(val)) std::invoke(std::forward<F>(f), std::move(error()))
(5,6) G() std::invoke(std::forward<F>(f), error())
(7,8) std::invoke(std::forward<F>(f), 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 自体を返し、そうでなければ変換された期待されない値を含む expected を返す
(public member function) [編集]
English 日本語 中文(简体) 中文(繁體)