Namespaces
Variants

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

From cppreference.net
Utilities library
プライマリテンプレート
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 * this の予期しない値を引数として呼び出し、その結果を返します。それ以外の場合、期待される値を表す std::expected オブジェクトを返します。

1-4) 期待値は val の期待値で初期化されます * this

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 < G :: value_type , T > false の場合、プログラムは不適格です。

1,2) これらのオーバーロードは、 std:: is_constructible_v < T, decltype ( ( val ) ) > true の場合にのみ、オーバーロード解決に参加します。
3,4) これらのオーバーロードは、以下の条件が満たされる場合にのみオーバーロード解決に参加します: std:: is_constructible_v < T, decltype ( std :: move ( val ) ) > true である場合。

目次

パラメータ

f - 適切な関数または Callable オブジェクトで、 std::expected を返すもの

戻り値

オーバーロード 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 argument-dependent lookup を引き起こす可能性がある。

関連項目

期待される値が含まれている場合は expected 自体を返す。それ以外の場合は、変換された予期しない値を含む expected を返す
(公開メンバ関数)