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

履歴 編集

function
<compare>

std::weak_ordering::operator==(C++20)

friend constexpr bool operator==(weak_ordering v, weak_ordering w) noexcept = default; // (1)

friend constexpr bool operator==(weak_ordering v, /*unspecified*/) noexcept;   // (2)

// (2)により、以下のオーバーロードが使用可能になる
friend constexpr bool operator==(/*unspecified*/, weak_ordering v) noexcept;   // (3)

概要

  • (1) : weak_ordering同士の等値比較を行う
  • (2)(3) : weak_orderingの値がweak_ordering::equivalentであるかを調べる。

戻り値

int型のメンバ変数valueに各有効値に対応する値を保持しているとして、以下と等価

  • (1) : return v.value == w.value
  • (2) : return v.value == 0
  • (3) : return v == 0

例外

投げない。

備考

この演算子により、以下の演算子が使用可能になる:

  • bool operator!=(weak_ordering v, weak_ordering w) noexcept;
  • bool operator!=(weak_ordering v, /*unspecified*/) noexcept;
  • bool operator!=(/*unspecified*/, weak_ordering w) noexcept;

unspecifiedとなっている片側の引数には0リテラルのみが使用できる。それ以外の物を渡した場合、動作は未定義

#include <iostream>
#include <compare>

int main()
{
  std::weak_ordering comp1 = 1 <=> 1;
  std::weak_ordering comp2 = 2 <=> 1;

  std::cout << std::boolalpha;

  // (1)
  std::cout << (comp1 == comp2) << std::endl;

  // (2) 
  std::cout << (comp1 == 0) << std::endl;

  // (3)
  std::cout << (0 == comp1) << std::endl;
}

出力

false
true
true

バージョン

言語

  • C++20

処理系

関連項目

参照