Namespaces
Variants

std:: three_way_comparable, std:: three_way_comparable_with

From cppreference.net
Utilities library
定义于头文件 <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 表达式 若使用了对某个常量左值操作数不可修改的表达式,则同样需要 隐式表达式变体

参见

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