Namespaces
Variants

std:: is_same

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
헤더에 정의됨 <type_traits>
template < class T, class U >
struct is_same ;
(C++11부터)

만약 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부터) 에 대한 특수화를 추가하는 경우, 그 동작은 정의되지 않습니다.

목차

헬퍼 변수 템플릿

template < class T, class U >
constexpr bool is_same_v = is_same < T, U > :: value ;
(C++17부터)

std:: integral_constant 로부터 상속됨

멤버 상수

value
[static]
true 만약 T U 가 동일한 타입이면, 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;
    // 일부 구현 정의 사실들
    // 일반적으로 'int'가 32비트일 때 참
    SHOW( std::is_same<int, std::int32_t>::value ); // 아마도 참
    // ILP64 데이터 모델을 사용할 경우 참일 수 있음
    SHOW( std::is_same<int, std::int64_t>::value ); // 아마도 거짓
    // 위와 동일한 테스트지만 C++17의 std::is_same_v<T, U> 형식 사용
    SHOW( std::is_same_v<int, std::int32_t> ); // 아마도 참
    SHOW( std::is_same_v<int, std::int64_t> ); // 아마도 거짓
    // 몇몇 변수들의 타입 비교
    long double num1 = 1.0;
    long double num2 = 2.0;
    static_assert( std::is_same_v<decltype(num1), decltype(num2)> == true );
    // 'float'은 절대 정수 타입이 아님
    static_assert( std::is_same<float, std::int32_t>::value == false );
    // 'int'는 암시적으로 '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 );
    // 다른 타입들과 달리 'char'은 'unsigned'도 '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 한정 타입 T는 비-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

참고 항목

(C++20)
한 타입이 다른 타입과 동일함을 명시
(concept)
decltype specifier (C++11) 표현식 또는 엔티티의 타입을 얻음