<type_traits>

类模板
<type_traits>

std::is_same

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

用于识别Tis the same type asU, including having the sameconstand/orvolatilequalification, if any.

Two different type names are considered to represent the same type if -and only if- one is atypedefof 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_sameinherits from integral_constant as being either true_type or false_type, depending on whetherTUare the same type.

模板参数

T, U
Types.

成员类型

Inherited from integral_constant
成员类型定义
value_typebool
类型true_type 或 false_type

成员常量

Inherited from integral_constant
成员常量定义
trueorfalse

成员函数

Inherited from integral_constant

示例

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;
}

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


另见