std::strong_ordering
來自 cppreference.com
定義於標頭檔案 <compare> |
||
class strong_ordering; |
(C++20 起) | |
類型別 std::strong_ordering
是三路比較的結果型別,它
- 允許所有六種關係運算符(
==
、!=
、<
、<=
、>
、>=
)。
- 隱含可替換性:如果 a 等價於 b,則 f(a) 也等價於 f(b),其中 f 表示一個函式,它只讀取可透過引數的公共 const 成員訪問的、與比較相關的狀態。換句話說,等價的值是無法區分的。
- 不允許不可比較的值:a < b、a == b 或 a > b 中恰好有一個必須為 true。
目錄 |
[編輯] 常量
型別 std::strong_ordering
有四個有效值,它們作為其型別的 const 靜態資料成員實現
名稱 | 定義 |
inline constexpr std::strong_ordering less [靜態] |
一個有效值,表示小於(排在前面)關係 (public static 成員常量) |
inline constexpr std::strong_ordering equivalent [靜態] |
一個有效值,表示等價(既不排在前面也不排在後面)關係,與 equal 相同(public static 成員常量) |
inline constexpr std::strong_ordering equal [靜態] |
一個有效值,表示等價(既不排在前面也不排在後面)關係,與 equivalent 相同(public static 成員常量) |
inline constexpr std::strong_ordering greater [靜態] |
一個有效值,表示大於(排在後面)關係 (public static 成員常量) |
[編輯] 轉換
std::strong_ordering
是三種比較類別中最強的一種:它不能從任何其他類別隱式轉換,但可以隱式轉換為其他兩種類別。
operator partial_ordering |
隱式轉換為 std::partial_ordering (公開成員函式) |
std::strong_ordering::operator partial_ordering
constexpr operator partial_ordering() const noexcept; |
||
返回值
如果 v
是 less
,則為 std::partial_ordering::less;如果 v
是 greater
,則為 std::partial_ordering::greater;如果 v
是 equal
或 equivalent
,則為 std::partial_ordering::equivalent。
operator weak_ordering |
隱式轉換為 std::weak_ordering (公開成員函式) |
std::strong_ordering::operator weak_ordering
constexpr operator weak_ordering() const noexcept; |
||
返回值
如果 v
是 less
,則為 std::weak_ordering::less;如果 v
是 greater
,則為 std::weak_ordering::greater;如果 v
是 equal
或 equivalent
,則為 std::weak_ordering::equivalent。
[編輯] 比較
此型別的值與字面量 0 之間定義了比較運算子。這支援表示式 a <=> b == 0 或 a <=> b < 0,這些表示式可用於將三路比較運算子的結果轉換為布林關係;參見 std::is_eq、std::is_lt 等。
這些函式對於普通的非限定查詢或限定查詢不可見,只有當 std::strong_ordering
是引數的關聯類時,才能透過實參依賴查詢找到它們。
試圖將 strong_ordering
與除了整數常量 0 之外的任何值進行比較的程式的行為是未定義的。
operator==operator<operator>operator<=operator>=operator<=> |
與零或 strong_ordering 進行比較(函式) |
operator==
friend constexpr bool operator==( strong_ordering v, /*unspecified*/ u ) noexcept; |
(1) | |
friend constexpr bool operator==( strong_ordering v, strong_ordering w ) noexcept = default; |
(2) | |
引數
v, w | - | 要檢查的 std::strong_ordering 值 |
u | - | 接受字面量零引數的任何型別的未使用引數 |
返回值
1) 如果
v
是 equivalent
或 equal
,則為 true;如果 v
是 less
或 greater
,則為 false2) 如果兩個引數持有相同的值,則為 true;否則為 false。注意
equal
與 equivalent
相同。operator<
friend constexpr bool operator<( strong_ordering v, /*unspecified*/ u ) noexcept; |
(1) | |
friend constexpr bool operator<( /*unspecified*/ u, strong_ordering v ) noexcept; |
(2) | |
引數
v | - | 要檢查的 std::strong_ordering 值 |
u | - | 接受字面量零引數的任何型別的未使用引數 |
返回值
1) 如果
v
是 less
,則為 true;如果 v
是 greater
、equivalent
或 equal
,則為 false2) 如果
v
是 greater
,則為 true;如果 v
是 less
、equivalent
或 equal
,則為 falseoperator<=
friend constexpr bool operator<=( strong_ordering v, /*unspecified*/ u ) noexcept; |
(1) | |
friend constexpr bool operator<=( /*unspecified*/ u, strong_ordering v ) noexcept; |
(2) | |
引數
v | - | 要檢查的 std::strong_ordering 值 |
u | - | 接受字面量零引數的任何型別的未使用引數 |
返回值
1) 如果
v
是 less
、equivalent
或 equal
,則為 true;如果 v
是 greater
,則為 false2) 如果
v
是 greater
、equivalent
或 equal
,則為 true;如果 v
是 less
,則為 falseoperator>
friend constexpr bool operator>( strong_ordering v, /*unspecified*/ u ) noexcept; |
(1) | |
friend constexpr bool operator>( /*unspecified*/ u, strong_ordering v ) noexcept; |
(2) | |
引數
v | - | 要檢查的 std::strong_ordering 值 |
u | - | 接受字面量零引數的任何型別的未使用引數 |
返回值
1) 如果
v
是 greater
,則為 true;如果 v
是 less
、equivalent
或 equal
,則為 false2) 如果
v
是 less
,則為 true;如果 v
是 greater
、equivalent
或 equal
,則為 falseoperator>=
friend constexpr bool operator>=( strong_ordering v, /*unspecified*/ u ) noexcept; |
(1) | |
friend constexpr bool operator>=( /*unspecified*/ u, strong_ordering v ) noexcept; |
(2) | |
引數
v | - | 要檢查的 std::strong_ordering 值 |
u | - | 接受字面量零引數的任何型別的未使用引數 |
返回值
1) 如果
v
是 greater
、equivalent
或 equal
,則為 true;如果 v
是 less
,則為 false2) 如果
v
是 less
、equivalent
或 equal
,則為 true;如果 v
是 greater
,則為 falseoperator<=>
friend constexpr strong_ordering operator<=>( strong_ordering v, /*unspecified*/ u ) noexcept; |
(1) | |
friend constexpr strong_ordering operator<=>( /*unspecified*/ u, strong_ordering v ) noexcept; |
(2) | |
引數
v | - | 要檢查的 std::strong_ordering 值 |
u | - | 接受字面量零引數的任何型別的未使用引數 |
返回值
1) v。
2) 如果
v
是 less
,則為 greater
;如果 v
是 greater
,則為 less
;否則為 v
。[編輯] 示例
執行此程式碼
#include <compare> #include <iostream> struct Point { int x{}, y{}; friend constexpr std::strong_ordering operator<=>(Point lhs, Point rhs) { if (lhs.x < rhs.x or (lhs.x == rhs.x and lhs.y < rhs.y)) return std::strong_ordering::less; if (lhs.x > rhs.x or (lhs.x == rhs.x and lhs.y > rhs.y)) return std::strong_ordering::greater; return std::strong_ordering::equivalent; } friend std::ostream& operator<<(std::ostream& os, Point s) { return os << '(' << s.x << ',' << s.y << ')'; } }; void print_three_way_comparison(const auto& p, const auto& q) { const auto cmp{p <=> q}; std::cout << p << (cmp < 0 ? " < " : cmp > 0 ? " > " : " == " ) // compares with 0 << q << '\n'; } void print_two_way_comparison(const auto& p, const auto& q) { std::cout << p << (p < q ? " < " : p > q ? " > " : " == ") // compares p and q << q << '\n'; } int main() { const Point p1{0, 1}, p2{0, 1}, p3{0, 2}; print_three_way_comparison(p1, p2); print_two_way_comparison(p1, p2); print_three_way_comparison(p2, p3); print_two_way_comparison(p2, p3); print_three_way_comparison(p3, p2); print_two_way_comparison(p3, p2); }
輸出
(0,1) == (0,1) (0,1) == (0,1) (0,1) < (0,2) (0,1) < (0,2) (0,2) > (0,1) (0,2) > (0,1)
[編輯] 另請參閱
(C++20) |
支援所有6個運算子但不可替換的三路比較的結果型別 (class) |
(C++20) |
支援所有6個運算子,不可替換,並允許不可比較值的三路比較的結果型別 (class) |