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
}
概要
basic_string_viewオブジェクトの三方比較を行う。
適格要件
戻り値の型R(traits::comparison_categoryが存在すればその型)が、比較カテゴリ型(strong_ordering・weak_ordering・partial_orderingのいずれか)であること。満たさない場合、この演算子の使用はプログラムを不適格とする。
戻り値
戻り値の型Rは、traits::comparison_categoryが存在していればその型、そうでなければweak_orderingとなり、以下と等価:
return static_cast<R>(x.compare(y) <=> 0);
備考
- この演算子により、以下の演算子が使用可能になる (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
処理系
- Clang:
- GCC: 10 ✅
- Visual C++: ??
参照
- P1614R2 The Mothership has Landed
- C++20での三方比較演算子の追加と、関連する演算子の自動導出
- LWG Issue 3950.
std::basic_string_viewcomparison operators are overspecified- C++26で、第2引数が
type_identity_tで包まれ、basic_string_viewへ暗黙変換可能な型と直接比較できるようになった(追加の比較オーバーロードの規定が不要になった)
- C++26で、第2引数が
- LWG Issue 3432. Missing requirement for
comparison_category- C++23で、戻り値型
Rが比較カテゴリ型でなければならないという要件(Mandates)が追加され、traits::comparison_categoryが妥当な型を示す場合にのみ使われることが明確化された
- C++23で、戻り値型