Namespaces
Variants

std:: compare_three_way_result

From cppreference.net
Utilities library
헤더 파일에 정의됨 <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 > 의 lvalue를 나타낸다고 할 때, 표현식 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

참고 항목

6개의 연산자를 모두 지원하고, 치환 가능하지 않으며, 비교 불가능한 값을 허용하는 3-way 비교의 결과 타입
(class)
6개의 연산자를 모두 지원하고 치환 가능하지 않은 3-way 비교의 결과 타입
(class)
6개의 연산자를 모두 지원하고 치환 가능한 3-way 비교의 결과 타입
(class)