Namespaces
Variants

std:: strong_ordering

From cppreference.net
Utilities library
定义于头文件 <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 - 接受字面量零参数的任何类型的未使用参数

返回值

1) true 如果 v equivalent equal false 如果 v less greater
2) true 如果两个参数持有相同的值, false 否则。注意 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 - 接受字面零参数的任何类型的未使用参数

返回值

1) true 如果 v less ,而 false 如果 v greater equivalent equal
2) true 如果 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 - 接受字面零参数的任何类型的未使用参数

返回值

1) true 如果 v less equivalent equal ,且 false 如果 v greater
2) true 如果 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 - 接受字面零参数的任意类型的未使用参数

返回值

1) true 如果 v greater ,若 v less equivalent equal 则返回 false
2) true 如果 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 - 接受字面零参数的任何类型的未使用参数

返回值

1) true 如果 v greater equivalent equal false 如果 v less
2) true 如果 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 - 接受字面量零参数的任何类型的未使用参数

返回值

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)

参见

支持全部6种运算符且不可替换的三路比较结果类型
(类)
支持全部6种运算符、不可替换且允许不可比较值的三路比较结果类型
(类)