<type_traits>

class template
<type_traits>

std::is_same

template <class T, class U> struct is_same;
Is same type

Trait class that identifies whether T is the same type as U, including having the same const and/or volatile qualification, if any.

Two different type names are considered to represent the same type if -and only if- one is a typedef of the other: Two names representing types with the exact same characteristics but which none is a typedef of the other are not considered the same type.

is_same inherits from integral_constant as being either true_type or false_type, depending on whether T and U are the same type.

Template parameters

T, U
Types.

Member types

Inherited from integral_constant:
member typedefinition
value_typebool
typeeither true_type or false_type

Member constants

Inherited from integral_constant:
member constantdefinition
valueeither true or false

Member functions

Inherited from integral_constant:

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// is_same example
#include <iostream>
#include <type_traits>
#include <cstdint>

typedef int integer_type;
struct A { int x,y; };
struct B { int x,y; };
typedef A C;

int main() {
  std::cout << std::boolalpha;
  std::cout << "is_same:" << std::endl;
  std::cout << "int, const int: " << std::is_same<int, const int>::value << std::endl;
  std::cout << "int, integer_type: " << std::is_same<int, integer_type>::value << std::endl;
  std::cout << "A, B: " << std::is_same<A,B>::value << std::endl;
  std::cout << "A, C: " << std::is_same<A,C>::value << std::endl;
  std::cout << "signed char, std::int8_t: " << std::is_same<signed char,std::int8_t>::value << std::endl;
  return 0;
}

Output:
is_same:
int, const int: false
int, integer_type: true
A, B: false
A, C: true
signed char, std::int8_t: true


See also