template<class T>
struct remove_const; 移除const
template<class T>
struct add_const; 添加const
template<class T>
struct remove_reference; 移除引用
template<class T>
struct add_lvalue_reference; 添加左值引用
template<class T>
struct add_rvalue_reference; 添加右值引用
template<class T>
struct remove_extent; 移除数组顶层的维度
template<class T>
struct remove_all_extent; 移除数组所有的维度
template<class T>
struct remove_pointer; 移除指针
template<class T>
struct add_pointer; 添加指针
template<class T>
struct decay; 移除cv(const volatile)或添加指针
template<class T>
struct common_type; 获取公共类型
使用如下:
cout << is_same<int, remove_reference<int&>::type>::value << endl; //1
cout << is_same<int, remove_reference<int&&>::type>::value << endl; //1
cout << is_same<int, remove_extent<int[]>::type>::value << endl; //1
cout << is_same<int[2], remove_extent<int[][2]>::type>::value << endl; //1
cout << is_same<int[2][3], remove_extent<int[][2][3]>::type>::value << endl; //1
cout << is_same<int, remove_all_extents<int[][2][3]>::type>::value << endl; //1
typedef common_type<unsigned char, short, int>::type NumberType;
cout << is_same<int, NumberType>::value << endl; //1
template<typename T>
typename remove_cv<typename remove_reference<T>::type>::type* Create()
{
typedef typename remove_cv<typename remove_reference<T>::type>::type U;
return new U();
}
或
template<typename T>
typename decay<T>::type* Create()
{
typedef typename decay<T>::type U;
return new U();
}
int* p = Create<const volatile int&>(); //ok
对于普通类型,decay是移除引用和cv符,除了普通类型,decay还可以用于数组和函数,具体规则如下:
先移除T类型的引用,得到类型U,U定义为remove_reference<T>::type
如果is_array<U>::value为true,修改类型type为remove_extent<U>::type
否则,如果is_function<U>::value为true,修改类型type为add_pointer<U>::type
否则,修改类型type为remove_cv<U>::type