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

履歴 編集

function
<expected>

std::expected::swap(C++23)

constexpr void swap(expected& rhs) noexcept(see below);

概要

他のexpectedオブジェクトとデータを入れ替える。

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

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

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

次の制約を全て満たすこと

効果

*thisrhsそれぞれが正常値/エラー値いずれを保持しているかに応じて、以下の効果を持つ。

戻り値

なし

例外

noexcept例外指定は次の式に従う :

is_nothrow_move_constructible_v<T> && is_nothrow_swappable_v<T> && is_nothrow_move_constructible_v<E> && is_nothrow_swappable_v<E>

noexcept例外指定がfalseの場合、上記の「効果」による処理からの例外がスローされる。

#include <cassert>
#include <expected>
#include <string>

int main()
{
  std::expected<int, std::string> x = 42;
  std::expected<int, std::string> y = std::unexpected{"ERR"};
  assert(x.value() == 42 && y.error() == "ERR");

  x.swap(y);
  assert(x.error() == "ERR" && y.value() == 42);
}

出力

バージョン

言語

  • C++23

処理系

参照