std::strong_ordering
来自cppreference.com
<tbody>
</tbody>
| 在标头 <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 静态数据成员:
| 名称 | 定义 |
less [静态] |
合法值,指示小于(先序)关系 (公开静态成员常量) |
equivalent [静态] |
合法值,指示等价(既非先序亦非后序),等于 equal (公开静态成员常量) |
equal [静态] |
合法值,指示等价(既非先序亦非后序),等于 equivalent (公开静态成员常量) |
greater [静态] |
合法值,指示大于(后序)关系 (公开静态成员常量) |
转换
std::strong_ordering 是三个比较类别的最强者:它不能从任何其他类别隐式转换,而能隐式转换到其他两种类别。
| 到 std::partial_ordering 的隐式转换 (公开成员函数) |
std::strong_ordering::operator partial_ordering
<tbody> </tbody> 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。
| 到 std::weak_ordering 的隐式转换 (公开成员函数) |
std::strong_ordering::operator weak_ordering
<tbody> </tbody> 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 的内容比较,则其行为未定义。
与零或 strong_order 比较 (函数) |
operator==
<tbody> </tbody> friend constexpr bool operator==( strong_ordering v, /* 未指明 */ u ) noexcept; |
(1) | |
friend constexpr bool 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<
<tbody> </tbody> friend constexpr bool operator<( strong_ordering v, /* 未指明 */ u ) noexcept; |
(1) | |
friend constexpr bool 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<=
<tbody> </tbody> friend constexpr bool operator<=( strong_ordering v, /* 未指明 */ u ) noexcept; |
(1) | |
friend constexpr bool 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>
<tbody> </tbody> friend constexpr bool operator>( strong_ordering v, /* 未指明 */ u ) noexcept; |
(1) | |
friend constexpr bool 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>=
<tbody> </tbody> friend constexpr bool operator>=( strong_ordering v, /* 未指明 */ u ) noexcept; |
(1) | |
friend constexpr bool 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<=>
<tbody> </tbody> friend constexpr strong_ordering operator<=>( strong_ordering v, /* 未指明 */ u ) noexcept; |
(1) | |
friend constexpr 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{};
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 种运算符且不可替换,并允许不可比较的值 (类) |