传送门 ==>> AutoSAR实战系列300讲「糖果Autosar」总目录
1 类模板声明
// cplusplus.com
template <class T> struct decay;
// MS C++ 2013
template <class _Ty>
struct decay
{ // determines decayed version of _Ty
...
};
// GCC 4.8.2
template <typename _Tp>
class decay
{
...
};
2 Std::Decay 类模板描述
Type T 应用左值到右值 (LVALUE-TO-RVALUE)、Array-to-Pointer with Function-to-Pointer隐式转换。转换将删除类型 T的 CV 限定符(Const 和 Volatile 限定符)并将结果类型的类型定义为成员衰减 < T >::Type。这种转换与传输所有参数时的转换非常相似。
- 如果类型 T 是函数类型,将应用从函数到指针的类型转换,T 的衰减类型等价于: add_pointer::type
- 如果类型 T 是数组类型,将应用从数组到指针的类型转换,T 的衰减类型等价于add_pointer<remove_extent<remove_reference::type>::type>::type
- 将左值应用到右值时,T 的衰减类型等价于:remove_cv<remove_reference::type>::type
3 模板参数说明
T : 某种类型。当t为引用类型时,DECAY<T>::Type返回T引用的元素类型;当 T 为非引用类型时,DECAY <T>::Type 返回 T 类型。
4 Std :: Decay基本类型示例
#include <iostream>
#include <type_traits>
using namespace std;
typedef decay<int>::type A; // A is int
typedef decay<int &>::type B; // B is int
typedef decay<int &&>::type C; // C is int
typedef decay<const int &>::type D; // D is int
typedef decay<int[2]>::type E; // E is int *
typedef decay<int(int)>::type F; // F is int(*)(int)
int main()
{
cout << boolalpha;
cout << is_same<int, A>::value << endl; // true
cout << is_same<int, B>::value << endl; // true
cout << is_same<int, C>::value << endl; // true
cout << is_same<int, D>::value << endl; // true
cout << is_same<int *, E>::value << endl; // true
cout << is_same<int(int), F>::value << endl; // false
cout << is_same<int(*)(int), F>::value << endl; // true
return 1;
}
5 Std :: Decay非基本类型示例
#include <iostream>
#include <type_traits>
using namespace std;
class MyClass {};
typedef decay<MyClass>::type A; // A is MyClass
typedef decay<MyClass &>::type B; // B is MyClass
typedef decay<MyClass &&>::type C; // C is MyClass
typedef decay<const MyClass &>::type D; // D is MyClass
typedef decay<MyClass[2]>::type E; // E is MyClass *
typedef decay<MyClass *>::type F; // E is MyClass *
typedef decay<MyClass *[2]>::type G; // G is MyClass **
typedef decay<MyClass **>::type H; // H is MyClass **
int main()
{
cout << boolalpha;
cout << is_same<MyClass, A>::value << endl; // true
cout << is_same<MyClass, B>::value << endl; // true
cout << is_same<MyClass, C>::value << endl; // true
cout << is_same<MyClass, D>::value << endl; // true
cout << is_same<MyClass *, E>::value << endl; // true
cout << is_same<MyClass *, F>::value << endl; // true
cout << is_same<MyClass **, G>::value << endl; // true
cout << is_same<MyClass **, H>::value << endl; // true
return 1;
}