Namespaces
Variants

std::weak_order

来自 zh.cppreference.net
 
 
 
定义于头文件 <compare>
inline namespace /* unspecified */ {
    inline constexpr /* unspecified */ weak_order = /* unspecified */;
}
(C++20 起)
调用签名
template< class T, class U >
    requires /* see below */
constexpr std::weak_ordering weak_order(T&& t, U&& u) noexcept(/* see below */);

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

设 t 和 u 为表达式,T 和 U 分别表示 decltype((t)) 和 decltype((u)) 的类型,std::weak_order(t, u) 是表达式等价于:

  • 如果 std::is_same_v<std::decay_t<T>, std::decay_t<U>> 是 true:
    • std::weak_ordering(weak_order(t, u)),如果它是在不含 std::weak_order 声明的上下文中执行重载决议的良构表达式,
    • 否则,如果 T 是浮点类型:
      • 如果 std::numeric_limits<T>::is_iec559 是 true,则执行浮点值的弱序比较(见下文)并将该结果作为类型 std::weak_ordering 的值返回,
      • 否则,产生一个类型为 std::weak_ordering 的值,该值与通过 T 的比较运算符观察到的顺序一致,
    • 否则,若 std::weak_ordering(std::compare_three_way()(t, u)) 为良构,则执行之,
    • 否则,若 std::weak_ordering(std::strong_order(t, u)) 为良构,则执行之。
  • 在所有其他情况下,该表达式为非良构,当它出现在模板实例化的立即上下文中时,可能导致替换失败。

定制点对象

名称 std::weak_order 表示一个 定制点对象 ,这是一个字面量 semiregular 类类型的常量 函数对象 。详细信息请参阅 定制点对象 。

IEEE浮点类型的严格弱序

设 x 和 y 为相同 IEEE 浮点类型的值,且 weak_order_less ( x, y ) 为布尔结果,指示在 C++ 标准定义的严格弱序中 x 是否先于 y 。

  • 如果 x 和 y 均非 NaN,则 weak_order_less ( x, y ) == true 当且仅当 x < y ,即所有相等浮点数值的表示形式均等价;
  • 如果 x 为负 NaN 且 y 非负 NaN,则 weak_order_less ( x, y ) == true ;
  • 如果 x 非正 NaN 且 y 为正 NaN,则 weak_order_less ( x, y ) == true ;
  • 如果 x 和 y 均为同号 NaN,则 ( weak_order_less ( x, y ) || weak_order_less ( y, x ) ) == false ,即所有同号 NaN 均等价。

示例

参见

支持全部6种运算符但不可替换的三路比较结果类型
(类)
执行三路比较并生成 std::strong_ordering 类型的结果
(定制点对象)
执行三路比较并生成 std::partial_ordering 类型的结果
(定制点对象)
执行三路比较并生成 std::weak_ordering 类型的结果,即使 operator <=> 不可用
(定制点对象)