std:: strong_ordering
|
定义于头文件
<compare>
|
||
|
class
strong_ordering
;
|
(C++20 起) | |
类类型
std::strong_ordering
是
三路比较
的结果类型,其特性为:
-
支持全部六种关系运算符(
==,!=,<,<=,>,>=)。
- 蕴含可替换性:若 a 等价于 b ,则 f ( a ) 也等价于 f ( b ) ,其中 f 表示仅读取通过实参的公开常量成员可访问的、与比较相关的状态的函数。换言之,等价的值是不可区分的。
- 不允许不可比较的值 : a < b 、 a == b 或 a > b 中必须有且仅有一个为 true 。
目录 |
常量
类型
std::strong_ordering
具有四个有效值,通过其类型的常量静态数据成员实现:
| 名称 | 定义 |
|
inline
constexpr
std
::
strong_ordering
less
[static]
|
表示小于(排序在前)关系的有效值
(公开静态成员常量) |
|
inline
constexpr
std
::
strong_ordering
equivalent
[static]
|
表示等价(既不排序在前也不排序在后)的有效值,与
equal
相同
(公开静态成员常量) |
|
inline
constexpr
std
::
strong_ordering
equal
[static]
|
表示等价(既不排序在前也不排序在后)的有效值,与
equivalent
相同
(公开静态成员常量) |
|
inline
constexpr
std
::
strong_ordering
greater
[static]
|
表示大于(排序在后)关系的有效值
(公开静态成员常量) |
转换
std::strong_ordering
是三种比较类别中最严格的:它不能从任何其他类别隐式转换而来,但可以隐式转换为其他两种类别。
|
operator partial_ordering
|
隐式转换为
std::partial_ordering
(公开成员函数) |
std::strong_ordering:: operator partial_ordering
|
constexpr
operator partial_ordering
(
)
const
noexcept
;
|
||
返回值
若
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
(
)
const
noexcept
;
|
||
返回值
若
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_ordering
进行比较
(函数) |
operator==
|
friend
constexpr
bool
operator == ( strong_ordering v, /*unspecified*/ u ) noexcept ; |
(1) | |
|
friend
constexpr
bool
operator == ( strong_ordering v, strong_ordering w ) noexcept = default ; |
(2) | |
参数
| v, w | - |
要检查的
std::strong_ordering
值
|
| u | - | 接受字面量零参数的任何类型的未使用参数 |
返回值
v
为
equivalent
或
equal
,
false
如果
v
为
less
或
greater
equal
与
equivalent
相同。
operator<
|
friend
constexpr
bool
operator
<
(
strong_ordering v,
/*unspecified*/
u
)
noexcept
;
|
(1) | |
|
friend
constexpr
bool
operator
<
(
/*unspecified*/
u, strong_ordering v
)
noexcept
;
|
(2) | |
参数
| v | - |
要检查的
std::strong_ordering
值
|
| u | - | 接受字面零参数的任何类型的未使用参数 |
返回值
v
为
less
,而
false
如果
v
为
greater
、
equivalent
或
equal
v
为
greater
,而
false
如果
v
为
less
、
equivalent
或
equal
operator<=
|
friend
constexpr
bool
operator
<=
(
strong_ordering v,
/*unspecified*/
u
)
noexcept
;
|
(1) | |
|
friend
constexpr
bool
operator
<=
(
/*unspecified*/
u, strong_ordering v
)
noexcept
;
|
(2) | |
参数
| v | - |
要检查的
std::strong_ordering
值
|
| u | - | 接受字面零参数的任何类型的未使用参数 |
返回值
v
为
less
、
equivalent
或
equal
,且
false
如果
v
为
greater
v
为
greater
、
equivalent
或
equal
,且
false
如果
v
为
less
operator>
|
friend
constexpr
bool
operator
>
(
strong_ordering v,
/*unspecified*/
u
)
noexcept
;
|
(1) | |
|
friend
constexpr
bool
operator
>
(
/*unspecified*/
u, strong_ordering v
)
noexcept
;
|
(2) | |
参数
| v | - |
要检查的
std::strong_ordering
值
|
| u | - | 接受字面零参数的任意类型的未使用参数 |
返回值
v
为
greater
,若
v
为
less
、
equivalent
或
equal
则返回
false
v
为
less
,若
v
为
greater
、
equivalent
或
equal
则返回
false
operator>=
|
friend
constexpr
bool
operator
>=
(
strong_ordering v,
/*unspecified*/
u
)
noexcept
;
|
(1) | |
|
friend
constexpr
bool
operator
>=
(
/*unspecified*/
u, strong_ordering v
)
noexcept
;
|
(2) | |
参数
| v | - |
要检查的
std::strong_ordering
值
|
| u | - | 接受字面零参数的任何类型的未使用参数 |
返回值
v
为
greater
、
equivalent
或
equal
,
false
如果
v
为
less
v
为
less
、
equivalent
或
equal
,
false
如果
v
为
greater
operator<=>
|
friend
constexpr
strong_ordering
operator <=> ( strong_ordering v, /*unspecified*/ u ) noexcept ; |
(1) | |
|
friend
constexpr
strong_ordering
operator <=> ( /*unspecified*/ u, strong_ordering v ) noexcept ; |
(2) | |
参数
| v | - |
要检查的
std::strong_ordering
值
|
| u | - | 接受字面量零参数的任何类型的未使用参数 |
返回值
v
为
less
则返回
greater
,若
v
为
greater
则返回
less
,否则返回
v
。
示例
#include <compare> #include <iostream> struct Point { int x{}, y{}; friend constexpr 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; } friend std::ostream& operator<<(std::ostream& os, Point s) { return os << '(' << s.x << ',' << s.y << ')'; } }; void print_three_way_comparison(const auto& p, const auto& q) { const auto cmp{p <=> q}; std::cout << p << (cmp < 0 ? " < " : cmp > 0 ? " > " : " == " ) // 与0比较 << q << '\n'; } void print_two_way_comparison(const auto& p, const auto& 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种运算符、不可替换且允许不可比较值的三路比较结果类型
(类) |