Skip to content

Latest commit

 

History

History
73 lines (56 loc) · 2.07 KB

op_equal.md

File metadata and controls

73 lines (56 loc) · 2.07 KB

operator==

  • utility[meta header]
  • std[meta namespace]
  • function template[meta id-type]
namespace std {
  template <class T1, class T2>
  bool operator==(const pair<T1, T2>& x, const pair<T1, T2>& y);           // (1) C++03

  template <class T1, class T2>
  constexpr bool operator==(const pair<T1, T2>& x, const pair<T1, T2>& y); // (1) C++14

  template <class T1, class T2>
  struct pair {
    friend constexpr bool operator==(const pair&, const pair&) = default; // (1) C++20

    friend constexpr bool operator==(const pair& x, const pair& y)        // (2) C++20
        requires (is_reference_v<T1> || is_reference_v<T2>);
  };
}
  • is_reference_v[link /reference/type_traits/is_reference.md]

概要

2つのpairの等値比較を行う

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

  • x.first == y.firstx.second == y.secondが妥当であり、型decltype(x.first == y.first)decltype(x.second == y.second)boolean-testableのモデルであること

戻り値

return x.first == y.first && x.second == y.second;

備考

  • この演算子により、以下の演算子が使用可能になる (C++20):
    • operator!=

#include <iostream>
#include <utility>
#include <string>

int main()
{
  std::pair<int, std::string> p1(1, "aaa");
  std::pair<int, std::string> p2(1, "aaa");
  std::pair<int, std::string> p3(2, "bbb");

  std::cout << std::boolalpha;
  std::cout << (p1 == p2) << std::endl;
  std::cout << (p1 == p3) << std::endl;
}

出力

true
false

参照