std::three_way_comparable, std::three_way_comparable_with

来自cppreference.com
< cpp‎ | utility
 
 
 
在标头 <compare> 定义
template<class T, class Cat =std::partial_ordering>

concept three_way_comparable =
    __WeaklyEqualityComparableWith<T, T>&&
    __PartiallyOrderedWith<T, T>&&
    requires(conststd::remove_reference_t<T>& a,
             conststd::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<
            conststd::remove_reference_t<T>&,
            conststd::remove_reference_t<U>&>, Cat>&&
    __WeaklyEqualityComparableWith<T, U>&&
    __PartiallyOrderedWith<T, U>&&
    requires(conststd::remove_reference_t<T>& t,
             conststd::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) 概念 three_way_comparable_with<T, U, Cat> 指定三路比较运算符 <=> 在(可能混合的) TU 操作数上生成与 Cat 所蕴含的比较类别一致的结果。比较混合的操作数生成的结果等价于比较转换到其公共类型的操作数。

__WeaklyEqualityComparableWith__PartiallyOrderedWith__ComparisonCommonTypeWith 是仅用于阐释的概念。参见 equality_comparabletotally_ordered 的说明。

目录

[编辑]语义要求

这些概念仅若其所蕴含的概念均被实现才得到实现。

1)TCat 实现 std::three_way_comparable<T, Cat>,仅若给定 conststd::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<conststd::remove_reference_t<T>&, conststd::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(C(t)<=> C(u))
  • (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 表达式,也会要求其隐式的表达式变种

[编辑]参阅

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