<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


另見