types_is_same
types_is_same (타입 동일성 판별)
std::is_same은 두 타입이 서로 동일한 타입인지 컴파일 타임에 판별해 주는 타입 특성(type trait)이에요. const나 volatile 한정까지 고려해서 완전히 같은 타입이면 value가 true가 되고, 그렇지 않으면 false가 돼요. C++11부터 사용할 수 있고, C++17부터는 is_same_v 변수 템플릿으로 더 간결하게 사용할 수 있어요.
출처: cppreference
본문
개요
<type_traits> 헤더에 정의되어 있어요.
template < class T , class U > struct is_same ;
T와 U가 동일한 타입을 가리키면(const/volatile 한정까지 고려), 멤버 상수 value는 true가 돼요. 그렇지 않으면 value는 false예요.
교환 법칙도 성립해요. 즉, 임의의 두 타입 T와 U에 대해 is_same<T, U>::value == true인 것은 is_same<U, T>::value == true인 것과 서로 동치예요.
프로그램에서 std::is_same이나 std::is_same_v(C++17부터)에 대해 특수화를 추가하면 동작이 정의되지 않아요(UB).
헬퍼 변수 템플릿
template < class T , class U > constexpr bool is_same_v = is_same < T , U >:: value ;
C++17부터 사용할 수 있어요.
std::integral_constant에서 상속받는 멤버
멤버 상수
| 이름 | 설명 |
|---|---|
value [static] |
T와 U가 동일한 타입이면 true, 아니면 false (public static member constant) |
멤버 함수
| 이름 | 설명 |
|---|---|
operator bool |
객체를 bool로 변환하고 value를 반환해요 (public member function) |
operator() (C++14) |
value를 반환해요 (public member function) |
멤버 타입
| 타입 | 정의 |
|---|---|
value_type |
bool |
type |
std::integral_constant<bool, value> |
가능한 구현
template < class T , class U > struct is_same : std :: false_type {};
template < class T > struct is_same < T , T > : std :: true_type {};
예제
#include <cstdint>
#include <iostream>
#include <type_traits>
#define SHOW(...) std::cout << #__VA_ARGS__ << " : " << __VA_ARGS__ << '\n'
int main()
{
std::cout << std::boolalpha;
// some implementation-defined facts
// usually true if 'int' is 32 bit
SHOW( std::is_same<int, std::int32_t>::value ); // maybe true
// possibly true if ILP64 data model is used
SHOW( std::is_same<int, std::int64_t>::value ); // maybe false
// same tests as above, except using C++17's std::is_same_v<T, U> format
SHOW( std::is_same_v<int, std::int32_t> ); // maybe true
SHOW( std::is_same_v<int, std::int64_t> ); // maybe false
// compare the types of a couple variables
long double num1 = 1.0;
long double num2 = 2.0;
static_assert( std::is_same_v<decltype(num1), decltype(num2)> == true );
// 'float' is never an integral type
static_assert( std::is_same<float, std::int32_t>::value == false );
// 'int' is implicitly 'signed'
static_assert( std::is_same_v<int, int> == true );
static_assert( std::is_same_v<int, unsigned int> == false );
static_assert( std::is_same_v<int, signed int> == true );
// unlike other types, 'char' is neither 'unsigned' nor 'signed'
static_assert( std::is_same_v<char, char> == true );
static_assert( std::is_same_v<char, unsigned char> == false );
static_assert( std::is_same_v<char, signed char> == false );
// const-qualified type T is not same as non-const T
static_assert( !std::is_same<const int, int>() );
}
#undef SHOW
가능한 출력:
std::is_same<int, std::int32_t>::value : true
std::is_same<int, std::int64_t>::value : false
std::is_same_v<int, std::int32_t> : true
std::is_same_v<int, std::int64_t> : false
같이 보기
| 항목 | 설명 |
|---|---|
decltype 지정자 (C++11) |
표현식이나 엔티티의 타입을 얻어요 |
same_as (C++20) |
한 타입이 다른 타입과 동일함을 명시하는 개념(concept)이에요 |
is_same_type (C++26) |
두 리플렉션이 동일한 타입을 나타내는지 확인하는 함수예요 |