std::weak_ordering

来自cppreference.com
< 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, /* 未指明 */ 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, /* 未指明 */ u ) noexcept;
(1)
friend constexpr bool operator<( /* 未指明 */ 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, /* 未指明 */ u ) noexcept;
(1)
friend constexpr bool operator<=( /* 未指明 */ 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, /* 未指明 */ u ) noexcept;
(1)
friend constexpr bool operator>( /* 未指明 */ 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, /* 未指明 */ u ) noexcept;
(1)
friend constexpr bool operator>=( /* 未指明 */ u, weak_ordering v ) noexcept;
(2)

参数

v - 要检查的 std::weak_ordering
u - 未使用的形参,具有接受字面零实参的任何类型

返回值

1)vgreaterequivalent 则为 true,若 vless 则为 false
1)vlessequivalent 则为 true,若 vgreater 则为 false

operator<=>

friend constexpr weak_ordering operator<=>( weak_ordering v, /* 未指明 */ u ) noexcept;
(1)
friend constexpr weak_ordering operator<=>( /* 未指明 */ u, weak_ordering v ) noexcept;
(2)

参数

v - 要检查的 std::weak_ordering
u - 未使用的形参,具有接受字面零实参的任何类型

返回值

1) v
2)vless 则为 greater,若 vgreater 则为 less,否则为 v

[编辑] 示例

[编辑] 参阅

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