命名空间
变体
操作

std::weak_ordering

来自 cppreference.cn
< cpp‎ | utility
 
 
 
定义于头文件 <compare>
class weak_ordering;
(自 C++20 起)

类类型 std::weak_ordering三路比较的结果类型,它:

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

内容

[编辑] 常量

类型 std::weak_ordering 具有三个有效值,实现为其类型的 const 静态数据成员

名称 定义
inline constexpr std::weak_ordering less
[静态]
一个有效值,指示小于(排序在前)关系
(公共静态成员常量)
inline constexpr std::weak_ordering equivalent
[静态]
一个有效值,指示等价(既不排序在前也不排序在后)
(公共静态成员常量)
inline constexpr std::weak_ordering greater
[静态]
一个有效值,指示大于(排序在后)关系
(公共静态成员常量)

[编辑] 转换

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;

返回值

如果 vless,则为 std::partial_ordering::less;如果 vgreater,则为 std::partial_ordering::greater;如果 vequivalent,则为 std::partial_ordering::equivalent

[编辑] 比较

在此类型的值和字面量 0 之间定义了比较运算符。这支持表达式 a <=> b == 0a <=> b < 0,它们可用于将三路比较运算符的结果转换为布尔关系;请参阅 std::is_eqstd::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) 如果 vequivalent,则为 true;如果 vlessgreater,则为 false
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) 如果 vless,则为 true;如果 vgreaterequivalent,则为 false
2) 如果 vgreater,则为 true;如果 vlessequivalent,则为 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) 如果 vlessequivalent,则为 true;如果 vgreater,则为 false
2) 如果 vgreaterequivalent,则为 true;如果 vless,则为 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) 如果 vgreater,则为 true;如果 vlessequivalent,则为 false
2) 如果 vless,则为 true;如果 vgreaterequivalent,则为 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) 如果 vgreaterequivalent,则为 true;如果 vless,则为 false
2) 如果 vlessequivalent,则为 true;如果 vgreater,则为 false

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) 如果 vless,则为 greater;如果 vgreater,则为 less;否则为 v

[编辑] 示例

[编辑] 参见

支持所有 6 个运算符且可替换的三路比较的结果类型
(类) [编辑]
支持所有 6 个运算符、不可替换且允许不可比较值的三路比较的结果类型
(类) [编辑]