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

履歴 編集

function template
<expected>

std::expected::operator!=(C++23)

// operator==により、下記オーバーロードが使用可能になる
template<class T2, class E2> requires (!is_void_v<T2>)
friend constexpr bool operator!=(const expected& x, const expected<T2, E2>& y); // (1)

template<class T2>
friend constexpr bool operator!=(const expected& x, const T2& v); // (2)
template<class T2>
friend constexpr bool operator!=(const T2& v, const expected& x); // (3)

template<class E2>
friend constexpr bool operator!=(const expected& x, const unexpected<E2>& e); // (4)
template<class E2>
friend constexpr bool operator!=(const unexpected<E2>& e, const expected& x); // (5)

概要

  • (1) : expectedオブジェクト同士の非等値比較を行う。
  • (2), (3) : expectedオブジェクトと正常値の非等値比較を行う。
  • (4), (5) : expectedオブジェクトとエラー値の非等値比較を行う。

適格要件

  • (1) : 式*x == *yおよび式x.error() == y.error()適格であり、各式の結果をboolへ変換可能であること。
  • (2), (3) : 式*x == v適格であり、その結果をboolへ変換可能であること。
  • (4), (5) : 式x.error() == e.error()適格であり、その結果をboolへ変換可能であること。

戻り値

#include <cassert>
#include <expected>

int main()
{
  std::expected<long, long>   x1 = 1;
  std::expected<short, short> y1 = 100;
  std::expected<long, long>   x2 = std::unexpected{1};
  std::expected<short, short> y2 = std::unexpected{100};

  // (1)
  assert(x1 != y1);
  assert(x2 != y2);
  assert(x1 != y2);
  assert(x2 != y1);

  // (2), (3)
  assert(x1 != 2);
  assert(2 != x1);

  // (4), (5)
  assert(x2 != std::unexpected{2});
  assert(std::unexpected{2} != x2);
}

出力

バージョン

言語

  • C++23

処理系

参照