std::equality_comparable
std::equality_comparable (동등 비교 가능 개념)
타입 T(또는 두 타입 T, U)의 ==·!= 비교 연산자가 동등성(equality)을 반영하는지 명세하는 개념이에요. ==는 피연산자들이 같을 때(그리고 그때만) true를 산출해요. C++20부터 있어요.
출처: cppreference
본문
<concepts> 헤더에 정의돼 있어요.
template< class T >
concept equality_comparable = __WeaklyEqualityComparableWith<T, T>; // (1)
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)
헬퍼 개념들(설명 전용, exposition only):
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)
- (1) 개념
std::equality_comparable은T의==·!=비교 연산자가 동등성을 반영함을 명세해요.==는 피연산자들이 같을 때(그리고 그때만)true를 산출해요. - (2) 개념
std::equality_comparable_with는 (어쩌면 혼합된)T와U피연산자의==·!=결과가 동등성과 일관적임을 명세해요. 혼합 피연산자 비교는 그들을 공통 타입으로 변환해 비교한 결과와 동등한 결과를 산출해요. - (3) 설명 전용 개념
__WeaklyEqualityComparableWith는T타입 객체와U타입 객체가 (어느 순서로든)==와!=로 서로 동등 비교될 수 있고, 비교 결과가 일관적임을 명세해요.
의미 요구사항
- (1)
std::equality_comparable<T>는T타입의 객체a,b가 주어졌을 때bool(a == b)가a와b가 같을 때(그리고 그때만)true일 때만 모델링돼요.a == b가 동등성 보존이어야 한다는 요구와 함께, 이는==가 대칭(symmetric)이고 추이적(transitive)이며, 다른 적어도 하나의 객체와 같은 모든 객체a에 대해 반사적(reflexive)임을 의미해요. - (2)
std::equality_comparable_with<T, U>는bool(t == u) == bool(CONVERT_TO<C>(t2) == CONVERT_TO<C>(u2))가 참일 때만 모델링돼요 (C는std::common_reference_t<...>).
동등성 보존
표준 라이브러리 개념의 requires 표현식에서 선언된 표현식들은 (달리 명시되지 않는 한) 동등성 보존이어야 해요.
참고 문헌
- C++23 표준 (ISO/IEC 14882:2024): 18.5.4 Concept equality_comparable [concept.equalitycomparable]
- C++20 표준 (ISO/IEC 14882:2020): 18.5.3 Concept equality_comparable [concept.equalitycomparable]