命名空间
变体
操作

std::three_way_comparable, std::three_way_comparable_with

来自 cppreference.cn
< cpp‎ | 工具
 
 
工具库
语言支持
类型支持(基本类型、RTTI)
库特性测试宏 (C++20)
程序工具
变参函数
协程支持 (C++20)
契约支持 (C++26)
三路比较
three_way_comparablethree_way_comparable_with
(C++20)(C++20)
(C++20)
(C++20)(C++20)(C++20)  
(C++20)(C++20)(C++20)

通用工具
关系运算符 (C++20 中弃用)
 
定义于头文件 <compare>
template< class T, class Cat = std::partial_ordering >

concept three_way_comparable =
    __WeaklyEqualityComparableWith<T, T> &&
    __PartiallyOrderedWith<T, T> &&
    requires(const std::remove_reference_t<T>& a,
             const std::remove_reference_t<T>& b) {
        { a <=> b } -> __ComparesAs<Cat>;

    };
(1) (C++20 起)
template< class T, class U, class Cat = std::partial_ordering >

concept three_way_comparable_with =
    std::three_way_comparable<T, Cat> &&
    std::three_way_comparable<U, Cat> &&
    __ComparisonCommonTypeWith<T, U> &&
    std::three_way_comparable<
        std::common_reference_t<
            const std::remove_reference_t<T>&,
            const std::remove_reference_t<U>&>, Cat> &&
    __WeaklyEqualityComparableWith<T, U> &&
    __PartiallyOrderedWith<T, U> &&
    requires(const std::remove_reference_t<T>& t,
             const std::remove_reference_t<U>& u) {
        { t <=> u } -> __ComparesAs<Cat>;
        { u <=> t } -> __ComparesAs<Cat>;

    };
(2) (C++20 起)
template< class T, class Cat >

concept __ComparesAs =

    std::same_as<std::common_comparison_category_t<T, Cat>, Cat>;
(3) (仅作说明*)
1) 概念 std::three_way_comparable 指定对 T 的三路比较运算符 <=> 产生与 Cat 隐含的比较类别一致的结果。
2) 概念 std::three_way_comparable_with 指定对(可能混合的)TU 操作数的三路比较运算符 <=> 产生与 Cat 隐含的比较类别一致的结果。比较混合操作数产生的结果等同于比较转换为其共同类型的操作数。

__WeaklyEqualityComparableWith__PartiallyOrderedWith__ComparisonCommonTypeWith 是仅用于阐释的概念。请参见 equality_comparabletotally_ordered 的描述。

目录

[编辑] 语义要求

这些概念仅当它们被满足并且它们所包含的所有概念都被建模时才被建模。

1) 仅当给定类型为 const std::remove_reference_t<T> 的左值 ab 时,TCat 才符合 std::three_way_comparable<T, Cat> 模型,并且以下条件成立:
  • (a <=> b == 0) == bool(a == b),
  • (a <=> b != 0) == bool(a != b),
  • ((a <=> b) <=> 0)(0 <=> (b <=> a)) 相等,
  • bool(a > b) == bool(b < a),
  • bool(a >= b) == !bool(a < b),
  • bool(a <= b) == !bool(b < a),
  • (a <=> b < 0) == bool(a < b),
  • (a <=> b > 0) == bool(a > b),
  • (a <=> b <= 0) == bool(a <= b),以及
  • (a <=> b >= 0) == bool(a >= b),以及
  • 如果 Cat 可转换为 std::strong_ordering,则 T 符合 totally_ordered 模型。
2) 仅当给定以下条件时,TUCat 才符合 std::three_way_comparable_with<T, U, Cat> 模型:

Cstd::common_reference_t<const std::remove_reference_t<T>&, const std::remove_reference_t<U>&>,且给定表达式 E 和类型 C,则 CONVERT_TO<C>(E)

(直至 C++23)
  • static_cast<const C&>(std::as_const(E))(如果这是有效表达式),
  • static_cast<const C&>(std::move(E))(否则)。
(C++23 起)

以下条件为真

  • t <=> uu <=> t 具有相同的域,
  • ((t <=> u) <=> 0)(0 <=> (u <=> t)) 相等,
  • (t <=> u == 0) == bool(t == u),
  • (t <=> u != 0) == bool(t != u),
  • Cat(t <=> u) == Cat(CONVERT_TO<C>(t2) <=> CONVERT_TO<C>(u2)),
  • (t <=> u < 0) == bool(t < u),
  • (t <=> u > 0) == bool(t > u),
  • (t <=> u <= 0) == bool(t <= u),
  • (t <=> u >= 0) == bool(t >= u),以及
  • 如果 Cat 可转换为 std::strong_ordering,则 TU 符合 std::totally_ordered_with<T, U> 模型。

[编辑] 相等性保持

标准库概念的 requires 表达式中声明的表达式必须是相等性保持的(除非另有说明)。

[编辑] 隐式表达式变体

使用对于某个常量左值操作数是非修改表达式的 requires 表达式 也需要隐式表达式变体

[编辑] 另请参见

指定运算符 == 是等价关系
(概念) [编辑]
指定类型上的比较运算符产生全序关系
(概念) [编辑]