compare_three_way_comparable
compare_three_way_comparable (삼방향 비교 가능)
이 페이지는 C++20에서 도입된 std::three_way_comparable 및 std::three_way_comparable_with 개념(concept)에 대해 설명해요. 이 개념들은 <=> 연산자가 기대한 비교 범주(Category)로 동작하는지 검증하는 데 사용돼요. 또한 std::three_way_comparable은 단일 타입에 대해, std::three_way_comparable_with는 서로 다른 두 타입에 대해 삼방향 비교가 가능한지 확인해요.
출처: cppreference
본문
정의 (Definition)
<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 > ; };
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 > ; };
template < class T , class Cat > concept __ComparesAs = std :: same_as < std :: common_comparison_category_t < T , Cat > , Cat > ;
__WeaklyEqualityComparableWith, __PartiallyOrderedWith, __ComparisonCommonTypeWith는 exposition-only 개념이에요. equality_comparable 및 totally_ordered의 설명을 참고하세요.
의미론적 요구 사항 (Semantic requirements)
이 개념들은 조건을 충족하고, 포함하는 모든 개념이 모델링될 때만 모델링돼요.
( 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와 t2는 각각 const std :: remove_reference_t < T > 및 std :: remove_reference_t < T > 타입의 서로 다른 동등한 객체를 나타내는 lvalue이고, u와 u2는 각각 const std :: remove_reference_t < U > 및 std :: remove_reference_t < U > 타입의 서로 다른 동등한 객체를 나타내는 lvalue예요. C를 std :: common_reference_t < const std :: remove_reference_t < T >& , const std :: remove_reference_t < U >&>라고 하고, 표현식 E와 타입 C가 주어졌을 때 CONVERT_TO < C > ( E )를 다음과 같이 정의해요.
static_cast < C > ( std :: as_const ( E )) . |
(C++23까지) |
|---|---|
static_cast < const C &> ( std :: as_const ( E )) (그것이 유효한 표현식이라면), 그렇지 않으면 static_cast < const C &> ( std :: move ( E )). |
(C++23부터) |
그리고 다음이 모두 참이어야 해요.
t <=> u와u <=> t는 동일한 영역(domain)을 가져요.(( 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 >를 모델링해요.
동등성 보존 (Equality preservation)
표준 라이브러리 개념의 requires 표현식에 선언된 표현식은 (특별히 명시되지 않는 한) 동등성을 보존해야 해요.
암시적 표현 변형 (Implicit expression variations)
어떤 상수 lvalue 피연산자에 대해 수정하지 않는 표현식을 사용하는 requires 표현식은 암시적 표현 변형(implicit expression variations)도 요구해요.
같이 보기 (See also)
equality_comparable,equality_comparable_with(C++20) —operator ==가 동치 관계임을 명시하는 개념totally_ordered,totally_ordered_with(C++20) — 타입의 비교 연산자가 전순서를 생성함을 명시하는 개념