compare_compare_three_way_result
compare_compare_three_way_result (삼방향 비교 결과 타입)
이 페이지는 C++20에서 도입된 std::compare_three_way_result에 대해 설명해요. 이 타입은 주어진 두 타입 T와 U에 대해 <=> 연산자를 사용했을 때의 결과 타입을 제공해요. 만약 표현식 t <=> u가 유효하지 않다면 멤버 타입 type은 정의되지 않아요.
출처: cppreference
본문
<compare> 헤더에 정의됨 |
||
|---|---|---|
template < class T , class U = T > struct compare_three_way_result ; |
(since C++20) |
t와 u를 각각 const std::remove_reference_t<T>와 const std::remove_reference_t<U> 타입의 lvalue라고 할 때, 표현식 t <=> u가 잘 구성되어 있으면 멤버 typedef type이 decltype(t <=> u)와 같은 타입으로 제공돼요. 그렇지 않으면 멤버 타입이 존재하지 않아요.
프로그램이 std::compare_three_way_result에 대해 특수화를 추가하면 동작이 정의되지 않아요.
멤버 타입
| 이름 | 정의 |
|---|---|
type |
T와 U의 const 한정 lvalue에 대한 operator <=> 의 결과 타입 |
헬퍼 타입
template < class T , class U = T > using compare_three_way_result_t = compare_three_way_result < T , U >:: type ; |
(since C++20) |
|---|
가능한 구현
// recommended by Casey Carter
// see also: https://github.com/microsoft/STL/pull/385#discussion_r357894054
template < class T , class U = T > using compare_three_way_result_t = decltype ( std :: declval < const std :: remove_reference_t < T >&> () <=> std :: declval < const std :: remove_reference_t < U >&> () );
template < class T , class U = T > struct compare_three_way_result {};
template < class T , class U > requires requires { typename compare_three_way_result_t < T , U > ; } struct compare_three_way_result < T , U > { using type = compare_three_way_result_t < T , U > ; };
예제
#include <compare>
#include <iostream>
#include <type_traits>
template<class Ord>
void print_cmp_type()
{
if constexpr (std::is_same_v<Ord, std::strong_ordering>)
std::cout << "strong ordering\n";
else if constexpr (std::is_same_v<Ord, std::weak_ordering>)
std::cout << "weak ordering\n";
else if constexpr (std::is_same_v<Ord, std::partial_ordering>)
std::cout << "partial ordering\n";
else
std::cout << "illegal comparison result type\n";
}
int main()
{
print_cmp_type<std::compare_three_way_result_t<int>>();
print_cmp_type<std::compare_three_way_result_t<double>>();
}
출력:
strong ordering
partial ordering
더 알아보기
partial_ordering (C++20) |
6개 연산자를 모두 지원하고 대체 가능하지 않으며 비교 불가능한 값을 허용하는 3방향 비교의 결과 타입 (class) [edit] |
|---|---|
weak_ordering (C++20) |
6개 연산자를 모두 지원하고 대체 가능하지 않은 3방향 비교의 결과 타입 (class) [edit] |
strong_ordering (C++20) |
6개 연산자를 모두 지원하고 대체 가능한 3방향 비교의 결과 타입 (class) [edit] |