operator==,!=,<,<=,>,>=,<=>(std::basic_string_view)
在標頭 <string_view> 定義
|
||
(1) | ||
template< class CharT, class Traits > constexpr bool operator==( std::basic_string_view<CharT,Traits> lhs, |
(C++17 起) (C++20 前) |
|
template< class CharT, class Traits > constexpr bool operator==( |
(C++20 起) | |
template< class CharT, class Traits > constexpr bool operator!=( std::basic_string_view<CharT,Traits> lhs, |
(2) | (C++17 起) (C++20 前) |
template< class CharT, class Traits > constexpr bool operator<( std::basic_string_view<CharT,Traits> lhs, |
(3) | (C++17 起) (C++20 前) |
template< class CharT, class Traits > constexpr bool operator<=( std::basic_string_view<CharT,Traits> lhs, |
(4) | (C++17 起) (C++20 前) |
template< class CharT, class Traits > constexpr bool operator>( std::basic_string_view<CharT,Traits> lhs, |
(5) | (C++17 起) (C++20 前) |
template< class CharT, class Traits > constexpr bool operator>=( std::basic_string_view<CharT,Traits> lhs, |
(6) | (C++17 起) (C++20 前) |
template< class CharT, class Traits > constexpr /*comp-cat*/ operator<=>( |
(7) | (C++20 起) |
比較兩個視圖。
通過 compare() 成員函數進行所有比較(其自身則以 Traits::compare()
定義):
- 若 lhs 與 rhs 的大小相等且 lhs 中每個字符等價於 rhs 在同一位置的字符,則兩個視圖相等。
- 比較順序按字典序進行——由等價於 std::lexicographical_compare 的函數進行比較。
實現對這些函數提供充足的額外 |
(C++20 前) |
三路比較運算符的返回類型(/*comp-cat*/)為 Traits::comparison_category,若該有限定標識合法並指代類型,否則為 std::weak_ordering。若 /*comp-cat*/ 不是比較類別類型,則程序非良構。
|
(C++20 起) |
目錄 |
[編輯] 參數
lhs, rhs | - | 要比較的視圖 |
[編輯] 返回值
[編輯] 複雜度
與視圖大小成線性。
[編輯] 註解
充足的額外重載可通過一個參數類型中的非推導語境實現。 |
(C++20 前) |
std::string_view、std::wstring_view、std::u8string_view、std::u16string_view 與 std::u32string_view 的三路比較結果類型為 std::strong_ordering。 std::type_identity_t 用於非推導語境,使得可隱式轉換為字符串視圖類型的參數能與字符串視圖比較。 |
(C++20 起) |
[編輯] 示例
#include <string_view> int main() { using namespace std::literals; static_assert(""sv == ""sv); static_assert(""sv == "", "C++20 前选择额外的重载。"); static_assert("" == ""sv, "C++20 前选择额外的重载。C++20 起使用重写候选。"); static_assert(!(""sv != ""sv), "C++20 起使用重写候选。"); static_assert(!(""sv != ""), "C++20 前选择额外的重载;C++20 起使用重写候选。"); static_assert(!("" != ""sv), "C++20 前选择额外的重载;C++20 起使用重写候选。"); }
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 出版時的行為 | 正確行為 |
---|---|---|---|
LWG 3432 | C++20 | 未要求 operator<=> 的返回類型為比較類別類型
|
已要求 |
LWG 3950 | C++20 | 仍然要求冗餘的額外重載 | 減小重載集 |