compare_type_order (타입 전체 순서 비교)
이 페이지는 C++26에서 도입된 std::type_order 클래스 템플릿에 대해 설명해요. 이 템플릿은 모든 타입에 대해 구현이 정의한 전체 순서(total ordering)를 제공하며, 두 타입 사이의 순서를 std::strong_ordering 값으로 나타내요. 템플릿 인자로는 불완전한 타입도 사용할 수 있어요.
출처: cppreference
본문
std::type_order는 <compare> 헤더에 정의되어 있어요. 이 템플릿은 두 타입 T와 U를 비교하여, 구현이 정의한 전체 순서에서 T가 U보다 앞서면 std::strong_ordering::less를, U가 T보다 앞서면 std::strong_ordering::greater를, 같은 타입이면 std::strong_ordering::equal을 값으로 제공해요.
이 순서는 std::type_info::before가 유도하는 순서와 일치할 필요는 없어요. 또한 템플릿 인자 T와 U는 불완전한 타입일 수 있어요. 프로그램이 std::type_order 또는 std::type_order_v에 대해 특수화를 추가하면, 그 프로그램은 ill-formed로 간주돼요.
헬퍼 변수 템플릿
| 템플릿 |
설명 |
template <class T, class U> constexpr std::strong_ordering type_order_v = type_order<T, U>::value; |
(C++26부터) type_order<T, U>::value를 편리하게 접근할 수 있는 변수 템플릿이에요. |
멤버 상수
| 이름 |
설명 |
value [static] |
위에서 설명한 대로 std::strong_ordering 타입의 값이에요. (공용 정적 멤버 상수) |
멤버 함수
| 이름 |
설명 |
operator std::strong_ordering |
객체를 std::strong_ordering으로 변환하며 value를 반환해요. (공용 멤버 함수) |
operator() |
value를 반환해요. (공용 멤버 함수) |
멤버 타입
| 타입 |
정의 |
value_type |
std::strong_ordering |
참고 사항
| 기능 테스트 매크로 |
값 |
표준 |
기능 |
__cpp_lib_type_order |
202506L |
(C++26) |
std::type_order, std::type_order_v |
예제
#include <compare>
#include <meta>
#include <print>
#include <type_traits>
template <typename T, typename U>
void print_type_order()
{
auto t{std::meta::display_string_of(^^T)};
auto u{std::meta::display_string_of(^^U)};
if constexpr (constexpr auto order{ std::type_order_v<T, U> }; order == 0)
std::println("“{}” and “{}” have the same order", t, u);
else
std::println("“{}” goes before “{}”", order < 0 ? t : u, order < 0 ? u : t);
}
int main()
{
template for (constexpr auto t : {^^int, ^^char, ^^int&, ^^const int, ^^long})
print_type_order<int, typename[:t:]>();
}
가능한 출력:
“int” and “int” have the same order
“char” goes before “int”
“int&” goes before “int”
“const int” goes before “int”
“int” goes before “long int”
같이 보기
| 이름 |
설명 |
strong_ordering (C++20) |
6개의 모든 연산자를 지원하고 대체 가능한 3방향 비교의 결과 타입이에요. (클래스) |
type_order (C++26) |
반영된 타입에 대해 3방향 비교를 수행해요. (함수) |
더 알아보기 (Learn more)
cppreference