std::strong_order
| 定義於標頭檔案 <compare> |
||
| inline namespace /* unspecified */ { inline constexpr /* unspecified */ strong_order = /* unspecified */; |
(C++20 起) | |
| 呼叫簽名 (Call signature) |
||
| template< class T, class U > requires /* 見下文 */ |
||
使用三路比較來比較兩個值,並生成型別為 std::strong_ordering 的結果。
令 t 和 u 為表示式,T 和 U 分別表示 decltype((t)) 和 decltype((u)),std::strong_order(t, u) 等價於
- 如果 std::is_same_v<std::decay_t<T>, std::decay_t<U>> 為 true
- std::strong_ordering(strong_order(t, u)),如果它是格式良好的表示式,且過載決議在一個不包含
std::strong_order宣告的上下文中執行, - 否則,如果
T是浮點型別- 如果 std::numeric_limits<T>::is_iec559 為 true,則執行 ISO/IEC/IEEE 60559 totalOrder 浮點值比較,並以 std::strong_ordering 型別的值返回結果(注意:此比較可以區分正負零以及具有不同表示的 NaN),
- 否則,產生一個 std::strong_ordering 型別的值,該值與
T的比較運算子所觀察到的順序一致,
- 否則,如果格式良好,則為 std::strong_ordering(std::compare_three_way()(t, u))。
- std::strong_ordering(strong_order(t, u)),如果它是格式良好的表示式,且過載決議在一個不包含
- 在所有其他情況下,該表示式格式錯誤,當它出現在模板例項化的直接上下文中時,可能導致 替換失敗。
目錄 |
自定義點物件
名稱 std::strong_order 表示一個定製點物件,它是一個 const 函式物件,屬於 字面量 semiregular 類型別。為便於說明,其 cv 非限定版本型別表示為 __strong_order_fn。
__strong_order_fn 的所有例項均相等。在相同引數上呼叫不同型別 __strong_order_fn 例項的效果是等價的,無論表示例項的表示式是左值還是右值,以及是否具有 const 限定(但是,volatile 限定的例項不要求是可呼叫的)。因此,std::strong_order 可以自由複製,並且其副本可以互換使用。
給定一組型別 Args...,如果 std::declval<Args>()... 滿足上述 std::strong_order 的引數要求,則 __strong_order_fn 建模
- std::invocable<__strong_order_fn, Args...>,
- std::invocable<const __strong_order_fn, Args...>,
- std::invocable<__strong_order_fn&, Args...>,以及
- std::invocable<const __strong_order_fn&, Args...>.
否則,__strong_order_fn 的任何函式呼叫運算子都不參與過載決議。
[編輯] IEEE 浮點型別的嚴格全序
令 x 和 y 是相同 IEEE 浮點型別的值,total_order_less(x, y) 是一個布林結果,指示 x 是否在 ISO/IEC/IEEE 60559 中由 totalOrder 定義的嚴格全序中位於 y 之前。
(total_order_less(x, y) || total_order_less(y, x)) == false 當且僅當 x 和 y 具有相同的位模式。
- 如果 x 和 y 都不是 NaN
- 如果 x < y,則 total_order_less(x, y) == true;
- 如果 x > y,則 total_order_less(x, y) == false;
- 如果 x == y,
- 如果 x 是負零而 y 是正零,total_order_less(x, y) == true,
- 如果 x 非零且 x 的指數字段小於 y 的,則 total_order_less(x, y) == (x > 0)(僅對十進位制浮點數有意義);
- 如果 x 或 y 是 NaN
- 如果 x 是負 NaN 且 y 不是負 NaN,則 total_order_less(x, y) == true,
- 如果 x 不是正 NaN 且 y 是正 NaN,則 total_order_less(x, y) == true,
- 如果 x 和 y 都是具有相同符號的 NaN,且 x 的尾數域小於 y 的,則 total_order_less(x, y) == !std::signbit(x)。
[編輯] 示例
| 本節不完整 原因:無示例 |
[編輯] 參閱
| (C++20) |
支援所有6個運算子並且可替換的三路比較的結果型別 (class) |
| (C++20) |
執行三向比較並生成型別為 std::weak_ordering 的結果(定製點物件) |
| (C++20) |
執行三路比較併產生 std::partial_ordering 型別的結果(定製點物件) |
執行三路比較併產生 std::strong_ordering 型別的結果,即使 operator<=> 不可用(定製點物件) |