最終更新日時(UTC):
が更新

履歴 編集

function
<expected>

std::expected.void::operator=(C++23)

// expected<cv void, E>部分特殊化
constexpr expected& operator=(const expected& rhs);    // (1)

constexpr expected& operator=(expected&& rhs) noexcept(see below); // (2)

template<class G>
constexpr expected& operator=(const unexpected<G>& e); // (3)

template<class G>
constexpr expected& operator=(unexpected<G>&& e);      // (4)

概要

  • (1) : コピー代入。
  • (2) : ムーブ代入。
  • (3) : 変換可能なunexpectedオブジェクトから、エラー値としてコピー代入。
  • (4) : 変換可能なunexpectedオブジェクトから、エラー値としてムーブ代入。

動作説明用のexpectedクラスメンバ変数として、下記を導入する。

  • unex : E型のエラー値。
  • has_val : bool型のフラグ変数。正常値を保持する場合はtrueに、エラー値を保持する場合はfalseとなる。

テンプレートパラメータ制約

効果

  • (1) : 次の処理と等価
    • thisrhsが共に正常値を保持していたら、なにもしない
    • thisが正常値を保持し、rhsがエラー値を保持していたら、construct_at(addressof(unex), rhs.unex); has_value = false;
    • thisがエラーを保持し、rhsが正常値を保持していたら、unexを破棄しhas_value = true;
    • thisrhsが共にエラー値を保持していたら、unex = rhs.error()
  • (2) : 次の処理と等価
    • thisrhsが共に正常値を保持していたら、なにもしない
    • thisが正常値を保持し、rhsがエラー値を保持していたら、construct_at(addressof(unex), std::move(rhs.unex)); has_value = false;
    • thisがエラーを保持し、rhsが正常値を保持していたら、unexを破棄しhas_value = true;
    • thisrhsが共にエラー値を保持していたら、unex = std::move(rhs.error())
  • (3) : 次の処理と等価
  • (4) : 次の処理と等価

戻り値

*this

例外

delete定義される条件

#include <cassert>
#include <expected>
#include <memory>
#include <string>
#include <tuple>
#include <utility>

// std::pair型から2要素std::tuple型へはコピー代入可能
using IntPair  = std::pair<int, int>;
using IntTuple = std::tuple<int, int>;

// std::unique_ptr型からstd::shared_ptr型へはムーブ代入可能
using UniquePtr = std::unique_ptr<int>;
using SharedPtr = std::shared_ptr<int>;

int main()
{
  // (1) コピー代入
  {
    std::expected<void, int> srcV;
    std::expected<void, int> dstV;
    dstV = srcV;
    assert(srcV.has_value() && dstV.has_value());

    std::expected<void, int> srcE = std::unexpected{42};
    std::expected<void, int> dstE;
    dstE = srcE;
    assert(!srcE.has_value() && !dstE.has_value());
    assert(srcE.error() == 42 && dstE.error() == 42);
  }

  // (2) ムーブ代入
  {
    std::expected<void, std::string> srcV;
    std::expected<void, std::string> dstV;
    dstV = std::move(srcV);
    assert(srcV.has_value() && dstV.has_value());

    std::expected<void, std::string> srcE = std::unexpected{"Oops"};
    std::expected<void, std::string> dstE;
    dstE = std::move(srcE);
    assert(!srcE.has_value() && !dstE.has_value());
    assert(dstE.error() == "Oops");
    // srcE.error()はstd::stringムーブ後の未規定の値
  }

  // (3) エラー値の変換コピー代入
  {
    std::unexpected<IntPair> src{IntPair{1, 2}};
    std::expected<void, IntTuple> dst;
    dst = src;
    assert(not dst.has_value());
    assert((dst.error() == IntTuple{1, 2}));
  }

  // (4) エラー値の変換ムーブ代入
  {
    std::unexpected<UniquePtr> src{std::make_unique<int>(42)};
    std::expected<void, SharedPtr> dst;
    dst = std::move(src);
    assert(not dst.has_value());
    assert(*dst.error() == 42);
  }
}

出力

バージョン

言語

  • C++23

処理系

参照