std::strong_ordering
来自cppreference.com
在标头 <compare> 定义 | ||
class strong_ordering; | (C++20 起) | |
类类型 std::strong_ordering
是三路比较的结果类型:
- 接纳所有六个关系运算符(
==
、!=
、<
、<=
、>
、>=
)。
- 隐含可替换性:若 a 等价于 b,则 f(a) 亦等价于 f(b),其中 f 所指代的函数仅读取可以经由实参的公开 const 成员访问的显著比较状态。换言之,等价的值不可区分。
- 不允许不可比较值:a < b、a == b 和 a > b 必然恰有一个为 true。
目录 |
[编辑]常量
std::strong_ordering
类型拥有四个合法值,实现为其类型的 const 静态数据成员:
名称 | 定义 |
inlineconstexpr std::strong_ordering less [静态] | 合法值,指示小于(先序)关系 (公开静态成员常量) |
inlineconstexpr std::strong_ordering equivalent [静态] | 合法值,指示等价(既非先序亦非后序),等于 equal (公开静态成员常量) |
inlineconstexpr std::strong_ordering equal [静态] | 合法值,指示等价(既非先序亦非后序),等于 equivalent (公开静态成员常量) |
inlineconstexpr std::strong_ordering greater [静态] | 合法值,指示大于(后序)关系 (公开静态成员常量) |
[编辑]转换
std::strong_ordering
是三个比较类别的最强者:它不能从任何其他类别隐式转换,而能隐式转换到其他两种类别。
operator partial_ordering | 到 std::partial_ordering 的隐式转换 (公开成员函数) |
std::strong_ordering::operator partial_ordering
constexpr operator partial_ordering()constnoexcept; | ||
返回值
若 v
为 less
则为 std::partial_ordering::less,若 v
为 greater
则为 std::partial_ordering::greater,若 v
为 equal
或 equivalent
则为 std::partial_ordering::equivalent。
operator weak_ordering | 到 std::weak_ordering 的隐式转换 (公开成员函数) |
std::strong_ordering::operator weak_ordering
constexpr operator weak_ordering()constnoexcept; | ||
返回值
若 v
为 less
则为 std::weak_ordering::less,若 v
为 greater
则为 std::weak_ordering::greater,若 v
为 equal
或 equivalent
则为 std::weak_ordering::equivalent。
[编辑]比较
此类型的值和字面量 0 之间定义了比较运算符。这支持用表达式 a <=> b ==0 或 a <=> b <0 把三路比较的结果转换为布尔关系;见 std::is_eq、std::is_lt 等。
这些函数对常规的无限定或有限定查找不可见,而只能在 std::strong_ordering
为实参的关联类时由实参依赖查找找到。
若程序试图将 strong_ordering
与任何异于整数字面量 0 的内容比较,则其行为未定义。
operator==operator<operator>operator<=operator>=operator<=> | 与零或 strong_order 比较 (函数) |
operator==
friendconstexprbool operator==( strong_ordering v, /* 未指明 */ u )noexcept; | (1) | |
friendconstexprbool operator==( strong_ordering v, strong_ordering w )noexcept=default; | (2) | |
参数
v, w | - | 要检查的 std::strong_ordering 值 |
u | - | 未使用的形参,具有接受字面零实参的任何类型 |
返回值
1) 若
v
为 equivalent
或 equal
则为 true,若 v
为 less
或 greater
则为 false。2) 若两个形参持有相同值则为 true,否则为 false。注意
equal
等于 equivalent
。 operator<
friendconstexprbool operator<( strong_ordering v, /* 未指明 */ u )noexcept; | (1) | |
friendconstexprbool operator<(/* 未指明 */ u, strong_ordering v )noexcept; | (2) | |
参数
v | - | 要检查的 std::strong_ordering 值 |
u | - | 未使用的形参,具有接受字面零实参的任何类型 |
返回值
1) 若
v
为 less
则为 true,若 v
为 greater
、equivalent
或 equal
则为 false。2) 若
v
为 greater
则为 true,若 v
为 less
、equivalent
或 equal
则为 false。 operator<=
friendconstexprbool operator<=( strong_ordering v, /* 未指明 */ u )noexcept; | (1) | |
friendconstexprbool operator<=(/* 未指明 */ u, strong_ordering v )noexcept; | (2) | |
参数
v | - | 要检查的 std::strong_ordering 值 |
u | - | 未使用的形参,具有接受字面零实参的任何类型 |
返回值
1) 若
v
为 less
、equivalent
或 equal
则为 true,若 v
为 greater
则为 false。2) 若
v
为 greater
、equivalent
或 equal
则为 true,若 v
为 less
则为 false。 operator>
friendconstexprbool operator>( strong_ordering v, /* 未指明 */ u )noexcept; | (1) | |
friendconstexprbool operator>(/* 未指明 */ u, strong_ordering v )noexcept; | (2) | |
参数
v | - | 要检查的 std::strong_ordering 值 |
u | - | 未使用的形参,具有接受字面零实参的任何类型 |
返回值
1) 若
v
为 greater
则为 true,若 v
为 less
、equivalent
或 equal
则为 false。2) 若
v
为 less
则为 true,若 v
为 greater
、equivalent
或 equal
则为 false。 operator>=
friendconstexprbool operator>=( strong_ordering v, /* 未指明 */ u )noexcept; | (1) | |
friendconstexprbool operator>=(/* 未指明 */ u, strong_ordering v )noexcept; | (2) | |
参数
v | - | 要检查的 std::strong_ordering 值 |
u | - | 未使用的形参,具有接受字面零实参的任何类型 |
返回值
1) 若
v
为 greater
、equivalent
或 equal
则为 true,若 v
为 less
则为 false。2) 若
v
为 less
、equivalent
或 equal
则为 true,若 v
为 greater
则为 false。 operator<=>
friendconstexpr strong_ordering operator<=>( strong_ordering v, /* 未指明 */ u )noexcept; | (1) | |
friendconstexpr strong_ordering operator<=>(/* 未指明 */ u, strong_ordering v )noexcept; | (2) | |
参数
v | - | 要检查的 std::strong_ordering 值 |
u | - | 未使用的形参,具有接受字面零实参的任何类型 |
返回值
1)v。
2) 若
v
为 less
则为 greater,若 v
为 greater
则为 less,否则为 v
。 [编辑]示例
运行此代码
#include <compare>#include <iostream> struct Point {int x{}, y{}; friendconstexpr std::strong_ordering operator<=>(Point lhs, Point rhs){if(lhs.x< rhs.x or (lhs.x== rhs.x and lhs.y< rhs.y))return std::strong_ordering::less;if(lhs.x> rhs.x or (lhs.x== rhs.x and lhs.y> rhs.y))return std::strong_ordering::greater;return std::strong_ordering::equivalent;} friendstd::ostream& operator<<(std::ostream& os, Point s){return os <<'('<< s.x<<','<< s.y<<')';}}; void print_three_way_comparison(constauto& p, constauto& q){constauto cmp{p <=> q};std::cout<< p <<(cmp <0?" < ": cmp >0?" > ":" == ")// 与 0 比较<< q <<'\n';} void print_two_way_comparison(constauto& p, constauto& q){std::cout<< p <<(p < q ?" < ": p > q ?" > ":" == ")// 比较 p 和 q<< q <<'\n';} int main(){const Point p1{0, 1}, p2{0, 1}, p3{0, 2}; print_three_way_comparison(p1, p2); print_two_way_comparison(p1, p2); print_three_way_comparison(p2, p3); print_two_way_comparison(p2, p3); print_three_way_comparison(p3, p2); print_two_way_comparison(p3, p2);}
输出:
(0,1) == (0,1) (0,1) == (0,1) (0,1) < (0,2) (0,1) < (0,2) (0,2) > (0,1) (0,2) > (0,1)
[编辑]参阅
(C++20) | 三路比较的结果类型,支持所有 6 种运算符且不可替换 (类) |
(C++20) | 三路比较的结果类型,支持所有 6 种运算符且不可替换,并允许不可比较的值 (类) |