std::three_way_comparable, std::three_way_comparable_with
| 在标头 <compare> 定义
|
||
| template< class T, class Cat = std::partial_ordering > concept three_way_comparable = |
(1) | (C++20 起) |
| template< class T, class U, class Cat = std::partial_ordering > concept three_way_comparable_with = |
(2) | (C++20 起) |
| template< class T, class Cat > concept __ComparesAs = |
(3) | (仅用于阐述*) |
std::three_way_comparable 指定三路比较运算符 <=> 在 T 上生成与 Cat 所蕴含的比较类别一致的结果。three_way_comparable_with<T, U, Cat> 指定三路比较运算符 <=> 在(可能混合的) T 与 U 操作数上生成与 Cat 所蕴含的比较类别一致的结果。比较混合的操作数生成的结果等价于比较转换到其公共类型的操作数。__WeaklyEqualityComparableWith、__PartiallyOrderedWith 和 __ComparisonCommonTypeWith 是仅用于阐释的概念。参见 equality_comparable 和 totally_ordered 的说明。
语义要求
这些概念仅若其所蕴含的概念均被实现才得到实现。
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。
T、U 及 Cat 实现 std::three_way_comparable_with<T, U, Cat> 仅若给定
-
t和t2分别为指代不同的相等对象的 const std::remove_reference_t<T> 和 std::remove_reference_t<T> 类型的左值, -
u和u2分别为指代不同的相等对象的 const std::remove_reference_t<U> 和 std::remove_reference_t<U> 类型的左值。
令 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 前) |
|
(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(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,则T与U实现 std::totally_ordered_with<T, U>。
相等性保持
标准库概念的 requires 表达式中声明的表达式都要求保持相等性(除非另外说明)。
隐式表达式变种
使用了不修改某常量左值操作数的表达式的 requires 表达式,也会要求其隐式的表达式变种。
参阅
| 指定运算符 == 为等价关系 (概念) | |
| 指定比较运算符在该类型上产生全序 (概念) |