命名空间
变体
操作

std::strong_ordering

来自 cppreference.cn
< cpp‎ | 工具
 
 
 
定义于头文件 <compare>
class strong_ordering;
(C++20 起)

类类型 std::strong_ordering三路比较的结果类型,它

  • 允许所有六种关系运算符(==!=<<=>>=)。
  • 隐含可替换性:如果 a 等价于 b,则 f(a) 也等价于 f(b),其中 f 表示一个函数,它只读取可通过参数的公共 const 成员访问的、与比较相关的状态。换句话说,等价的值是无法区分的。
  • 不允许不可比较的值a < ba == ba > b 中恰好有一个必须为 true

目录

[编辑] 常量

类型 std::strong_ordering 有四个有效值,它们作为其类型的 const 静态数据成员实现

名称 定义
inline constexpr std::strong_ordering less
[静态]
一个有效值,表示小于(排在前面)关系
(public static 成员常量)
inline constexpr std::strong_ordering equivalent
[静态]
一个有效值,表示等价(既不排在前面也不排在后面)关系,与 equal 相同
(public static 成员常量)
inline constexpr std::strong_ordering equal
[静态]
一个有效值,表示等价(既不排在前面也不排在后面)关系,与 equivalent 相同
(public static 成员常量)
inline constexpr std::strong_ordering greater
[静态]
一个有效值,表示大于(排在后面)关系
(public static 成员常量)

[编辑] 转换

std::strong_ordering 是三种比较类别中最强的一种:它不能从任何其他类别隐式转换,但可以隐式转换为其他两种类别。

operator partial_ordering
隐式转换为 std::partial_ordering
(公开成员函数)

std::strong_ordering::operator partial_ordering

constexpr operator partial_ordering() const noexcept;

返回值

如果 vless,则为 std::partial_ordering::less;如果 vgreater,则为 std::partial_ordering::greater;如果 vequalequivalent,则为 std::partial_ordering::equivalent

operator weak_ordering
隐式转换为 std::weak_ordering
(公开成员函数)

std::strong_ordering::operator weak_ordering

constexpr operator weak_ordering() const noexcept;

返回值

如果 vless,则为 std::weak_ordering::less;如果 vgreater,则为 std::weak_ordering::greater;如果 vequalequivalent,则为 std::weak_ordering::equivalent

[编辑] 比较

此类型的值与字面量 0 之间定义了比较运算符。这支持表达式 a <=> b == 0a <=> b < 0,这些表达式可用于将三路比较运算符的结果转换为布尔关系;参见 std::is_eqstd::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) 如果 vequivalentequal,则为 true;如果 vlessgreater,则为 false
2) 如果两个参数持有相同的值,则为 true;否则为 false。注意 equalequivalent 相同。

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) 如果 vless,则为 true;如果 vgreaterequivalentequal,则为 false
2) 如果 vgreater,则为 true;如果 vlessequivalentequal,则为 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) 如果 vlessequivalentequal,则为 true;如果 vgreater,则为 false
2) 如果 vgreaterequivalentequal,则为 true;如果 vless,则为 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) 如果 vgreater,则为 true;如果 vlessequivalentequal,则为 false
2) 如果 vless,则为 true;如果 vgreaterequivalentequal,则为 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) 如果 vgreaterequivalentequal,则为 true;如果 vless,则为 false
2) 如果 vlessequivalentequal,则为 true;如果 vgreater,则为 false

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) 如果 vless,则为 greater;如果 vgreater,则为 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 ? " >  " : " == " ) // compares with 0
              << q << '\n';
}
 
void print_two_way_comparison(const auto& p, const auto& q)
{
    std::cout << p
              << (p < q ? " <  " : p > q ? " >  " : " == ") // compares p and 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个操作符但不可替换的三路比较的结果类型
(class) [编辑]
支持所有6个操作符,不可替换,并允许不可比较值的三路比较的结果类型
(class) [编辑]