std::compare_three_way_result
cppreference.net에서 가져옴
| 헤더 <compare>
|
||
template< class T, class U = T >
struct compare_three_way_result;
|
에 정의됨 (C++20부터) | |
t 과 u 이 각각 const std::remove_reference_t<T> 과 const std::remove_reference_t<U> 의 좌변값을 나타낸다고 할 때, 표현식 t <=> u 이 올바른 형식이면 멤버 typedef type 을 제공하며, 이는 decltype(t <=> u) 와 같고, 그렇지 않으면 멤버 type 가 없습니다.
프로그램이 std::compare_three_way_result 에 대해 특수화를 추가하는 경우, 동작은 정의되지 않습니다.
멤버 타입
| 이름 | 정의 |
type
|
const 한정자를 가진
T
와
U
의 좌측값에 대한
operator
<=>
연산자의 결과 타입
|
헬퍼 타입
|
template
<
class
T,
class
U
=
T
>
using compare_three_way_result_t = compare_three_way_result < T, U > :: type ; |
(C++20부터) | |
가능한 구현
// Casey Carter가 권장 // 참고: 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
참고 항목
|
(C++20)
|
6개의 연산자를 모두 지원하고, 치환 가능하지 않으며, 비교 불가능한 값을 허용하는 3-way 비교의 결과 타입
(class) |
|
(C++20)
|
6개의 연산자를 모두 지원하고 치환 가능하지 않은 3-way 비교의 결과 타입
(class) |
|
(C++20)
|
6개의 연산자를 모두 지원하고 치환 가능한 3-way 비교의 결과 타입
(class) |