Skip to content

Latest commit

 

History

History
77 lines (62 loc) · 2.27 KB

File metadata and controls

77 lines (62 loc) · 2.27 KB

operator==

  • string_view[meta header]
  • std[meta namespace]
  • function template[meta id-type]
  • cpp17[meta cpp]
namespace std {
  template <class CharT, class Traits>
  constexpr bool
    operator==(basic_string_view<CharT, Traits> x,
               basic_string_view<CharT, Traits> y) noexcept; // (1) C++17
  template <class CharT, class Traits>
  constexpr bool
    operator==(basic_string_view<CharT, Traits> x,
               type_identity_t<basic_string_view<CharT, Traits>> y) noexcept; // (1) C++26
}
  • type_identity_t[link /reference/type_traits/type_identity.md]

概要

basic_string_viewオブジェクトの等値比較を行う。

戻り値

return x.compare(y) == 0;
  • compare[link compare.md]

備考

  • この演算子により、以下の演算子が使用可能になる (C++20):
    • operator!=
  • C++26では、第2引数がtype_identity_tで包まれて非推論文脈となる。これにより、basic_string_viewへ暗黙変換可能な型(basic_stringや文字列リテラルなど)と直接比較できるようになり、従来別途規定されていた追加の比較オーバーロードが不要になった。

#include <iostream>
#include <string_view>

int main()
{
  std::string_view a = "aaa";
  std::string_view b {"aaaBB", 3}; // 先頭3文字を参照

  if (a == b) {
    std::cout << "equal" << std::endl;
  }
  else {
    std::cout << "not equal" << std::endl;
  }
}

出力

equal

バージョン

言語

  • C++17

処理系

参照