Namespaces
Variants

std:: equality_comparable, std:: equality_comparable_with

From cppreference.net
定义于头文件 <concepts>
template < class T >
concept equality_comparable = __WeaklyEqualityComparableWith < T, T > ;
(1) (C++20 起)
template < class T, class U >

concept equality_comparable_with =
std :: equality_comparable < T > &&
std :: equality_comparable < U > &&
__ComparisonCommonTypeWith < T, U > &&
std :: equality_comparable <
std:: common_reference_t <
const std:: remove_reference_t < T > & ,
const std:: remove_reference_t < U > & >> &&

__WeaklyEqualityComparableWith < T, U > ;
(2) (C++20 起)
辅助概念
template < class T, class U >

concept __WeaklyEqualityComparableWith =
requires ( const std:: remove_reference_t < T > & t,
const std:: remove_reference_t < U > & u ) {
{ t == u } - > boolean-testable ;
{ t ! = u } - > boolean-testable ;
{ u == t } - > boolean-testable ;
{ u ! = t } - > boolean-testable ;

} ;
(3) ( 仅用于说明* )
(4)
template < class T, class U >

concept __ComparisonCommonTypeWith =
std:: common_reference_with <
const std:: remove_reference_t < T > & ,

const std:: remove_reference_t < U > & > ;
(C++23 前)
( 仅用于说明* )
template < class T, class U, class C = std:: common_reference_t < const T & , const U & > >

concept _ComparisonCommonTypeWithImpl =
std:: same_as < std:: common_reference_t < const T & , const U & > ,
std:: common_reference_t < const U & , const T & >> &&
requires {
requires std:: convertible_to < const T & , const C & > ||
std:: convertible_to < T, const C & > ;
requires std:: convertible_to < const U & , const C & > ||
std:: convertible_to < U, const C & > ;
} ;
template < class T, class U >
concept __ComparisonCommonTypeWith =

_ComparisonCommonTypeWithImpl < std:: remove_cvref_t < T > , std:: remove_cvref_t < U >> ;
(C++23 起)
( 仅用于说明* )
1) 概念 std::equality_comparable 规定了类型 T 的比较运算符 == != 必须反映等价关系:当且仅当操作数相等时, == 应返回 true
2) 概念 std::equality_comparable_with 规定:对(可能混合的) T U 操作数进行比较操作符 == != 时,产生的结果与等价性一致。混合操作数的比较结果应等价于将操作数转换为其公共类型后的比较结果。
3) 仅用于阐述的概念 __WeaklyEqualityComparableWith 规定:类型 T 的对象与类型 U 的对象可以使用 == != 相互进行相等性比较(以任意顺序),且比较结果具有一致性。
4) 仅用于阐述的概念 __ComparisonCommonTypeWith 规定两种类型共享一个公共类型,且其中任一类型的 const 左值 或非 const 右值 (C++23 起) 可转换为该公共类型。

目录

语义要求

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

在以下段落中,给定表达式 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 起)
1) std :: equality_comparable < T > 仅在满足以下条件时被建模:给定类型 T 的对象 a b bool ( a == b ) true 当且仅当 a b 相等。结合 a == b 需满足 等值保持性 的要求,这意味着 == 具有对称性和传递性,并且进一步表明对于所有至少与另一个对象相等的对象 a == 具有自反性。
2) std :: equality_comparable_with < T, U > 仅在满足以下条件时被建模:令

则以下表达式为真:

  • bool ( t == u ) == bool ( CONVERT_TO < C > ( t2 ) == CONVERT_TO < C > ( u2 ) )
3) __WeaklyEqualityComparableWith < T, U > 仅在给定以下条件时成立:

以下表达式均为真:

  • t == u u == t t ! = u u ! = t 具有相同的定义域;
  • bool ( u == t ) == bool ( t == u )
  • bool ( t ! = u ) == ! bool ( t == u ) ;以及
  • bool ( u ! = t ) == bool ( t ! = u )
4) __WeaklyEqualityComparableWith < T, U > 仅在以下情况下被建模:

对应的 common_reference_with 概念被建模。

(C++23 前)

需满足以下条件:

  • CONVERT_TO < C > ( t1 ) 等于 CONVERT_TO < C > ( t2 ) 当且仅当 t1 等于 t2 ;且
  • CONVERT_TO < C > ( u1 ) 等于 CONVERT_TO < C > ( u2 ) 当且仅当 u1 等于 u2
(C++23 起)

等值保持性

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

隐式表达式变体

对于某个常量左值操作数使用非修改表达式的 requires 表达式 同样需要 隐式表达式变体

参考文献

  • C++23 标准 (ISO/IEC 14882:2024):
  • 18.5.4 概念 equality_comparable [concept.equalitycomparable]
  • C++20 标准 (ISO/IEC 14882:2020):
  • 18.5.3 概念 equality_comparable [concept.equalitycomparable]