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<=> 不可用(定制点对象) |