命名空间
变体
操作

std::strong_ordering

来自 cppreference.cn
< cpp‎ | utility
 
 
 
定义于头文件 <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 static 数据成员

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

[编辑] 转换

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 个运算符且不可替换的三路比较的结果类型
(类) [编辑]
支持全部 6 个运算符、不可替换且允许不可比较值的三路比较的结果类型
(类) [编辑]