Namespaces
Variants

std::three_way_comparable, std::three_way_comparable_with

来自 zh.cppreference.net
 
 
 
在头文件 <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 指定三路比较运算符 <=> 在(可能混合的)T 和 U 操作数上的结果与 Cat 所隐含的比较类别一致。混合操作数的比较结果等价于将操作数转换为其公共类型后进行比较的结果。

__WeaklyEqualityComparableWith、__PartiallyOrderedWith 和 __ComparisonCommonTypeWith 是仅用于阐述的概念。参见 equality_comparable 和 totally_ordered 的描述。

语义要求

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

1) T 和 Cat 满足 std :: three_way_comparable < T, Cat > 概念当且仅当,对于类型为 const std:: remove_reference_t < T > 的左值 a 和 b ,以下条件成立:
  • ( 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) T 、 U 和 Cat 满足 std :: three_way_comparable_with < T, U, Cat > 概念,当且仅当给定:

令 C 为 std:: 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 <=> u 和 u <=> 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 ,则 T 和 U 满足 std:: totally_ordered_with < T, U > 概念。

等值保持性

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

隐式表达式变体

一个 requires 表达式 若使用了对某个常量左值操作数不可修改的表达式,则同样需要 隐式表达式变体 。

参见

规定 == 运算符为等价关系
(概念)
规定该类型的比较运算符产生全序关系
(概念)