std::weak_ordering
來自 cppreference.com
定義於標頭檔案 <compare> |
||
class weak_ordering; |
(C++20 起) | |
類型別 std::weak_ordering
是 三路比較 的結果型別,它
- 允許所有六種關係運算符(
==
、!=
、<
、<=
、>
、>=
)。
- 不暗示可替代性:如果 a 等價於 b,則 f(a) 可能不等價於 f(b),其中 f 表示一個函式,它只讀取可透過引數的 public const 成員訪問的比較顯著狀態。換句話說,等價的值可能可區分。
- 不允許不可比較的值:a < b、a == b 或 a > b 中恰好有一個必須為 true。
目錄 |
[編輯] 常量
型別 std::weak_ordering
有三個有效值,以其型別的 const static 資料成員實現
名稱 | 定義 |
inline constexpr std::weak_ordering less [靜態] |
一個有效值,表示小於(排在前面)關係 (public static 成員常量) |
inline constexpr std::weak_ordering equivalent [靜態] |
一個表示等價的有效值(既不排在前面也不排在後面) (public static 成員常量) |
inline constexpr std::weak_ordering greater [靜態] |
一個有效值,表示大於(排在後面)關係 (public static 成員常量) |
[編輯] 轉換
std::weak_ordering
可以隱式轉換為 std::partial_ordering,而 std::strong_ordering 可以隱式轉換為 weak_ordering。
operator partial_ordering |
隱式轉換為 std::partial_ordering (公開成員函式) |
std::weak_ordering::operator partial_ordering
constexpr operator partial_ordering() const noexcept; |
||
返回值
如果 v
是 less
,則為 std::partial_ordering::less;如果 v
是 greater
,則為 std::partial_ordering::greater;如果 v
是 equivalent
,則為 std::partial_ordering::equivalent。
[編輯] 比較
在此型別的值和字面量 0 之間定義了比較運算子。這支援表示式 a <=> b == 0 或 a <=> b < 0,它們可用於將三路比較運算子的結果轉換為布林關係;參見 std::is_eq、std::is_lt 等。
這些函式對於普通的 非限定查詢 或 限定查詢 不可見,只能透過 引數依賴查詢 找到,當 std::weak_ordering
是引數的關聯類時。
嘗試將 weak_ordering
與非整數文字 0 進行比較的程式的行為是未定義的。
operator==operator<operator>operator<=operator>=operator<=> |
與零或 weak_ordering 進行比較(函式) |
operator==
friend constexpr bool operator==( weak_ordering v, /*unspecified*/ u ) noexcept; |
(1) | |
friend constexpr bool operator==( weak_ordering v, weak_ordering w ) noexcept = default; |
(2) | |
引數
v, w | - | 要檢查的 std::weak_ordering 值 |
u | - | 接受字面量零引數的任何型別的未使用引數 |
返回值
1) 如果
v
是 equivalent
,則為 true;如果 v
是 less
或 greater
,則為 false2) 如果兩個引數持有相同的值,則為 true;否則為 false
operator<
friend constexpr bool operator<( weak_ordering v, /*unspecified*/ u ) noexcept; |
(1) | |
friend constexpr bool operator<( /*unspecified*/ u, weak_ordering v ) noexcept; |
(2) | |
引數
v | - | 要檢查的 std::weak_ordering 值 |
u | - | 接受字面量零引數的任何型別的未使用引數 |
返回值
1) 如果
v
是 less
,則為 true;如果 v
是 greater
或 equivalent
,則為 false2) 如果
v
是 greater
,則為 true;如果 v
是 less
或 equivalent
,則為 falseoperator<=
friend constexpr bool operator<=( weak_ordering v, /*unspecified*/ u ) noexcept; |
(1) | |
friend constexpr bool operator<=( /*unspecified*/ u, weak_ordering v ) noexcept; |
(2) | |
引數
v | - | 要檢查的 std::weak_ordering 值 |
u | - | 接受字面量零引數的任何型別的未使用引數 |
返回值
1) 如果
v
是 less
或 equivalent
,則為 true;如果 v
是 greater
,則為 false2) 如果
v
是 greater
或 equivalent
,則為 true;如果 v
是 less
,則為 falseoperator>
friend constexpr bool operator>( weak_ordering v, /*unspecified*/ u ) noexcept; |
(1) | |
friend constexpr bool operator>( /*unspecified*/ u, weak_ordering v ) noexcept; |
(2) | |
引數
v | - | 要檢查的 std::weak_ordering 值 |
u | - | 接受字面量零引數的任何型別的未使用引數 |
返回值
1) 如果
v
是 greater
,則為 true;如果 v
是 less
或 equivalent
,則為 false2) 如果
v
是 less
,則為 true;如果 v
是 greater
或 equivalent
,則為 falseoperator>=
friend constexpr bool operator>=( weak_ordering v, /*unspecified*/ u ) noexcept; |
(1) | |
friend constexpr bool operator>=( /*unspecified*/ u, weak_ordering v ) noexcept; |
(2) | |
引數
v | - | 要檢查的 std::weak_ordering 值 |
u | - | 接受字面量零引數的任何型別的未使用引數 |
返回值
1) 如果
v
是 greater
或 equivalent
,則為 true;如果 v
是 less
,則為 false2) 如果
v
是 less
或 equivalent
,則為 true;如果 v
是 greater
,則為 falseoperator<=>
friend constexpr weak_ordering operator<=>( weak_ordering v, /*unspecified*/ u ) noexcept; |
(1) | |
friend constexpr weak_ordering operator<=>( /*unspecified*/ u, weak_ordering v ) noexcept; |
(2) | |
引數
v | - | 要檢查的 std::weak_ordering 值 |
u | - | 接受字面量零引數的任何型別的未使用引數 |
返回值
1) v。
2) 如果
v
是 less
,則為 greater
;如果 v
是 greater
,則為 less
;否則為 v
。[編輯] 示例
本節不完整 原因:無示例 |
[編輯] 參見
(C++20) |
支援所有6個運算子並且可替換的三路比較的結果型別 (類) |
(C++20) |
支援所有6個運算子,不可替換,並允許不可比較值的三路比較的結果型別 (類) |