Skip to content

Latest commit

 

History

History
87 lines (68 loc) · 2.93 KB

File metadata and controls

87 lines (68 loc) · 2.93 KB

operator<=>

  • string_view[meta header]
  • std[meta namespace]
  • function template[meta id-type]
  • cpp20[meta cpp]
namespace std {
  template<class charT, class traits>
  constexpr see below
    operator<=>(basic_string_view<charT, traits> x,
                basic_string_view<charT, traits> y) noexcept; // (1) C++20
  template<class charT, class traits>
  constexpr see below
    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オブジェクトの三方比較を行う。

適格要件

戻り値の型Rtraits::comparison_categoryが存在すればその型)が、比較カテゴリ型(strong_orderingweak_orderingpartial_orderingのいずれか)であること。満たさない場合、この演算子の使用はプログラムを不適格とする。

戻り値

戻り値の型Rは、traits::comparison_categoryが存在していればその型、そうでなければweak_orderingとなり、以下と等価:

return static_cast<R>(x.compare(y) <=> 0);
  • compare[link compare.md]

備考

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

#include <iostream>
#include <string_view>

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

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

出力

equal

バージョン

言語

  • C++20

処理系

参照