版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/kwinway/article/details/82084944
类型萃取用到的特性
1.模版的特化
2.在不同的特化版本中,将两个结构体 False和True 都typedef成了_IsPODType
/使用TypeTraits<T>::_IsPODType 不同版本的特化就会取到False版本的或者True版本的结构体
注意的一点是 在模版中使用 :: 作用域限定符时,如果::后面是一个自定义类型名,一定要
在前面加上typename,因为模版不知道::后面是一个静态变量还是类型
typename TypeTraits<T>::_IsPODType s;
#include<iostream>
struct _TrueType
{
};
struct _FalseType
{
};
template<class _Tp>
struct TypeTraits
{
typedef _FalseType _IsPODType;
};
template<>
struct TypeTraits <bool>
{
typedef _TrueType _IsPODType;
};
template<>
struct TypeTraits<char>
{
typedef _TrueType _IsPODType;
};
template<>
struct TypeTraits<int>
{
typedef _TrueType _IsPODType;
};
template<typename T>
void Copy(T*dst,T*src,size_t size,_FalseType)
{
std::cout << "is not pod tpye";
}
template<typename T>
void Copy(T* dst,T* src,size_t size,_TrueType)
{
std::cout << "is pod tpye";
};
template<typename T>
void Copy(T* dst,T*src,size_t size)
{
typename TypeTraits<T>::_IsPODType s;
Copy(dst,src,size,s);
}
int main()
{
int *a = new int(3);
int *b = new int(3);
Copy(a,b,1);
}