<type_traits>

類模板
<type_traits>

std::decay

template <class T> struct decay;
Decay type
Obtains the decay type ofT.

轉換後的類型別名為成員型別decay::type.

The decay type ofTis the same type that results from the standard conversions that happen when an lvalue expression is used as an rvalue, with its cv-qualifier stripped


This resembles the implicit conversions happening when an argument is passed by value to a function.

請注意,此類僅使用另一種型別作為模型來獲取型別,但它不會在這些型別之間轉換值或物件。

模板引數

T
一個型別。

成員型別

成員型別定義
型別如果已知Tis a reference type, the type referrered to byT.
否則,T.

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// decay example
#include <iostream>
#include <type_traits>

typedef std::decay<int>::type A;           // int
typedef std::decay<int&>::type B;          // int
typedef std::decay<int&&>::type C;         // int
typedef std::decay<const int&>::type D;    // int
typedef std::decay<int[2]>::type E;        // int*
typedef std::decay<int(int)>::type F;      // int(*)(int)

typedef int X[3];

int main() {
  std::cout << std::boolalpha;
  std::cout << "typedefs of int:" << std::endl;
  std::cout << "A: " << std::is_same<int,A>::value << std::endl;
  std::cout << "B: " << std::is_same<int,B>::value << std::endl;
  std::cout << "C: " << std::is_same<int,C>::value << std::endl;
  std::cout << "D: " << std::is_same<int,D>::value << std::endl;
  std::cout << "E: " << std::is_same<int,E>::value << std::endl;
  std::cout << "F: " << std::is_same<int,F>::value << std::endl;

  return 0;
}

輸出
typedefs of int:
A: true
B: true
C: true
D: true
E: false
F: false


另見