命名空间
变体
操作

std::three_way_comparable, std::three_way_comparable_with

来自 cppreference.cn
< cpp‎ | utility
 
 
 
定义于头文件 <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) TCat 建模 std::three_way_comparable<T, Cat> 仅当,给定类型为 const std::remove_reference_t<T> 的左值 ab 时,以下为真
  • (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 表达式,对于某些常量左值操作数使用非修改表达式,也需要隐式表达式变体

[编辑] 参见

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