Namespaces
Variants

std::weak_ordering

来自 zh.cppreference.net
 
 
 
定义于头文件 <compare>
class weak_ordering;
(C++20 起)

类类型 std::weak_ordering 是以下三路比较的结果类型:

  • 允许所有六种关系运算符(==、!=、<、<=、>、>=)。
  • 不蕴含可替换性:如果 a 等价于 b,f(a) 可能不等价于 f(b),其中 f 表示一个只读取可通过参数公共常成员访问的比较显著性状态的函数。换句话说,等价的值可能是可区分的。
  • 不允许不可比较的值:a < b、a == b 或 a > b 中恰好有一个必须为 true。

常量

类型 std::weak_ordering 具有三个有效值,通过其类型的常量静态数据成员实现:

名称 定义
inline constexpr std :: weak_ordering less
[static]
表示小于(排序在前)关系的有效值
(公开静态成员常量)
inline constexpr std :: weak_ordering equivalent
[static]
表示等价(既不排序在前也不排序在后)的有效值
(公开静态成员常量)
inline constexpr std :: weak_ordering greater
[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) true 如果 v 为 equivalent , false 如果 v 为 less 或 greater
2) 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) true 如果 v 为 less , false 如果 v 为 greater 或 equivalent
2) true 如果 v 为 greater , false 如果 v 为 less 或 equivalent

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) true 如果 v 为 less 或 equivalent , false 如果 v 为 greater
2) true 如果 v 为 greater 或 equivalent , false 如果 v 为 less

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) true 如果 v 为 greater , false 如果 v 为 less 或 equivalent
2) true 如果 v 为 less , false 如果 v 为 greater 或 equivalent

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) true 如果 v 为 greater 或 equivalent , false 如果 v 为 less
2) true 如果 v 为 less 或 equivalent , false 如果 v 为 greater

operator<=>

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 。

示例

参见

支持全部6种运算符且可替换的三路比较结果类型
(类)
支持全部6种运算符、不可替换且允许不可比较值的三路比较结果类型
(类)