Namespaces
Variants

std::expected<T,E>::operator=

From 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)



 
 
constexpr expected& operator=( const expected& other );
(1) (C++23以降)
constexpr expected& operator=( expected&& other ) noexcept(/*see below*/);
(2) (C++23以降)
template< class U = std::remove_cv_t<T> >
constexpr expected& operator=( U&& v );
(3) (C++23以降)
(Tはcv voidではない)
template< class G >
constexpr expected& operator=( const unexpected<G>& other );
(4) (C++23以降)
template< class G >
constexpr expected& operator=( unexpected<G>&& other );
(5) (C++23以降)

既存のexpectedオブジェクトに新しい値を代入します。

1,2) 他のotherの状態を代入します。
  • もしthis->has_value()がother.has_value()と等しい場合、otherに含まれる値を代入します。もしTが(cv修飾された可能性のある)voidであり、かつother.has_value()がtrueである場合は、何もしません。
  • そうでなければ、現在保持している値を破棄し(もしthis->has_value()がtrueであり、かつTが(cv修飾された可能性のある)voidである場合は何もしません)、*thisにotherに含まれる値のコピーを保持させます。
もしother.has_value()がtrueであり、かつTが(cv修飾された可能性のある)voidである場合、新しい値は構築されません。そうでなければ、新しい値は、状況に応じて、(1)の場合はコピー構築、または(2)の場合はムーブ構築され、そのソースは*otherまたはother.error()です。例外が発生した場合、古い値は保持されます;*thisは値なし状態になりません。

例外が発生しなかった場合、代入後、has_value()はother.has_value()と等しくなります。

  • オーバーロード(1)は、以下を満たさない限り、削除されたものとして定義されます:
    • 次のいずれか:Tが(cv修飾された可能性のある)voidである、またはstd::is_copy_assignable_v<T>がtrueである。かつ
    • 次のいずれか:Tが(cv修飾された可能性のある)voidである、またはstd::is_copy_constructible_v<T>がtrueである。かつ
    • std::is_copy_assignable_v<E>はtrueである。かつ
    • std::is_copy_constructible_v<E>はtrueである。かつ
    • 次の少なくとも1つがtrueである:
      • Tが(cv修飾された可能性のある)void
      • std::is_nothrow_move_constructible_v<T>
      • std::is_nothrow_move_constructible_v<E>
  • である オーバーロード(2)は、以下を満たす場合にのみオーバーロード解決に参加します:
    • 次のいずれか:Tが(cv修飾された可能性のある)voidである、またはstd::is_move_assignable_v<T>がtrueである。かつ
    • 次のいずれか:Tが(cv修飾された可能性のある)voidである、またはstd::is_move_constructible_v<T>がtrueである。かつ
    • std::is_move_assignable_v<E>はtrueである。かつ
    • std::is_move_constructible_v<E>はtrueである。かつ
    • 次の少なくとも1つがtrueである:
      • Tが(cv修飾された可能性のある)void
      • std::is_nothrow_move_constructible_v<T>
      • std::is_nothrow_move_constructible_v<E>
である 3) 期待される値から代入します。
  • もしthis->has_value()がtrueの場合、**this = std::forward<U>(v)と等価です。
  • そうでなければ、*thisに保持されている値を破棄し、*thisにstd::forward<U>(v)から初期化された値を保持させます。例外が発生した場合、古い値は保持されます;*thisは値なし状態になりません。

例外が発生しなかった場合、代入後、this->has_value()はtrueです。

  • このオーバーロードは、以下を満たす場合にのみオーバーロード解決に参加します:
    • std::is_same_v<expected, std::remove_cvref_t<U>>はfalseである。かつ
    • std::remove_cvref_t<U>はstd::unexpectedの特殊化ではない。かつ
    • std::is_constructible_v<T, U>はtrueである。かつ
    • std::is_assignable_v<T&, U>はtrueである。かつ
    • 次の少なくとも1つがtrueである:
      • std::is_nothrow_constructible_v<T, U>
      • std::is_nothrow_move_constructible_v<T>
      • std::is_nothrow_move_constructible_v<E>
4,5) 予期しない値から代入します。

GFを、オーバーロードconst G&(4)の場合はとし、オーバーロードG(5)の場合はとします。

  • もしthis->has_value()がtrueの場合、*thisに保持されている値を破棄し(もしTが(cv修飾された可能性のある)voidである場合は何もしません)、*thisにstd::forward<GF>(e.error())から初期化された値を保持させます。例外が発生した場合、古い値は保持されます;*thisは値なし状態になりません。
  • そうでなければ、this->error() = std::forward<GF>(e.error())と等価です。

例外が発生しなかった場合、代入後、this->has_value()はfalseです。

  • このオーバーロードは、以下を満たす場合にのみオーバーロード解決に参加します:
    • std::is_constructible_v<E, GF>はtrueである。かつ
    • std::is_assignable_v<E&, GF>はtrueである。かつ
    • 次の少なくとも1つがtrueである:
      • Tが(cv修飾された可能性のある)void
      • std::is_nothrow_constructible_v<E, GF>
      • std::is_nothrow_move_constructible_v<T>
      • std::is_nothrow_move_constructible_v<E>


である すべての場合において、もしTが(cv修飾された可能性のある)voidでなければ、古い値の破棄と新しい値の構築は、以下の説明専用関数reinit_expectedによって行われたかのように実行されます。

template<class NewType, class OldType, class... Args>
constexpr void reinit_expected(NewType& new_val, OldType& old_val, Args&&... args)
{
    if constexpr (std::is_nothrow_constructible_v<NewType, Args...>)
    {
        std::destroy_at(std::addressof(old_val));
        std::construct_at(std::addressof(new_val), std::forward<Args>(args)...);
    }
    else if constexpr (std::is_nothrow_move_constructible_v<NewType>)
    {
        NewType temp(std::forward<Args>(args)...); // may throw
        std::destroy_at(std::addressof(old_val));
        std::construct_at(std::addressof(new_val), std::move(temp));
    }
    else
    {
        OldType temp(std::move(old_val));
        std::destroy_at(std::addressof(old_val));
        try
        {
            std::construct_at(std::addressof(new_val), std::forward<Args>(args)...); // may throw
        }
        catch (...)
        {
            std::construct_at(std::addressof(old_val), std::move(temp));
            throw;
        }
    }
}

パラメータ

other - 代入する値を含む別の expected オブジェクト
v - 含まれる値に代入する値
e - std::unexpected オブジェクト(代入する値を含む)
newval - 構築される含まれる値
oldval - 破棄される含まれる値
args - newval の初期化子として使用される引数

戻り値

1-9) * this

例外

例

欠陥報告

以下の動作変更に関する欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 3886 C++23 オーバーロード ( 3 ) のデフォルトテンプレート引数は T であった std:: remove_cv_t < T > に変更
LWG 4025 C++23 オーバーロード ( 7 ) は E がムーブ構築可能でないか
ムーブ代入可能でない場合に削除定義されていた
この場合、オーバーロード解決に
参加しない

関連項目

期待される値をその場で構築する
(公開メンバ関数)