命名空间
变体
操作

std::strong_order

来自 cppreference.cn
< cpp‎ | utility
 
 
 
定义于头文件 <compare>
inline namespace /* 未指定 */ {

    inline constexpr /* 未指定 */ strong_order = /* 未指定 */;

}
(自 C++20 起)
调用签名
template< class T, class U >

    requires /* 见下文 */

constexpr std::strong_ordering strong_order( T&& t, U&& u ) noexcept(/* 见下文 */);

使用三路比较比较两个值,并生成 std::strong_ordering 类型的结果。

tu 为表达式,TU 分别表示 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_iec559true,则执行 ISO/IEC/IEEE 60559 totalOrder 浮点值比较,并将结果作为 std::strong_ordering 类型的值返回(注意:此比较可以区分正零和负零,以及不同表示的 NaN),
      • 否则,产生一个 std::strong_ordering 类型的值,该值与 T 的比较运算符观察到的顺序一致,
    • 否则,如果 std::strong_ordering(std::compare_three_way()(t, u)) 形式良好。
  • 在所有其他情况下,表达式是非良构的,当它出现在模板实例化的直接上下文中时,可能会导致替换失败

内容

自定义点对象

名称 std::strong_order 表示一个自定义点对象,它是一个 函数对象 的常量,该函数对象属于 字面量 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 建模

否则,__strong_order_fn 的函数调用运算符均不参与重载解析。

[编辑] IEEE 浮点类型的严格全序

xy 为相同 IEEE 浮点类型的值,total_order_less(x, y) 是一个布尔结果,指示 x 是否在 ISO/IEC/IEEE 60559 的 totalOrder 定义的严格全序中位于 y 之前。

(total_order_less(x, y) || total_order_less(y, x)) == false 当且仅当 xy 具有相同的位模式时。

  • 如果 xy 都不是 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) (仅对十进制浮点数有意义);
  • 如果 xy 是 NaN
    • 如果 x 是负 NaN,y 不是负 NaN,则 total_order_less(x, y) == true
    • 如果 x 不是正 NaN,y 是正 NaN,则 total_order_less(x, y) == true
    • 如果 xy 都是具有相同符号的 NaN,并且 x 的尾数字段小于 y 的尾数字段,则 total_order_less(x, y) == !std::signbit(x)

[编辑] 示例

[编辑] 参见

支持所有 6 个运算符且可替换的三路比较的结果类型
(类) [编辑]
执行三路比较并生成 std::weak_ordering 类型的结果
(自定义点对象)[编辑]
执行三路比较并生成 std::partial_ordering 类型的结果
(自定义点对象)[编辑]
执行三路比较并生成 std::strong_ordering 类型的结果,即使 operator<=> 不可用
(自定义点对象)[编辑]