c++ 类型萃取(模板类型 运用)

//类型萃取
#pragma once

#include<iostream>

using namespace std;

struct __TrueType//定义类 普通类型(基本类型的)
{
	bool Get()
	{
		return true;
	}
};

struct __FalseType//定义类 非基本类型
{
	bool Get()
	{
		return false;
	}
};

template <class _Tp>//模板类 (类型萃取)
struct TypeTraits //非基本类型
{
	typedef __FalseType   __IsPODType;
}; 
//将基本类型类 与非基本类型类 重命名(typedef)为相同名
 //调用__IsPODType.Get() 时编译器会根据TypeTraits<class T> T 的实际类型调用 	
//__FalseType	 或 Truetype 的 Get()函数  得到不同 bool值				
struct TypeTraits< bool>			  //	以下为常用基本类型特化
{
	typedef __TrueType     __IsPODType;
};

template <>
struct TypeTraits< char>
{
	typedef __TrueType     __IsPODType;
};

template <>
struct TypeTraits< unsigned char >
{
	typedef __TrueType     __IsPODType;
};

template <>
struct TypeTraits< short>
{
	typedef __TrueType     __IsPODType;
};

template <>
struct TypeTraits< unsigned short >
{
	typedef __TrueType     __IsPODType;
};

template <>
struct TypeTraits< int>
{
	typedef __TrueType     __IsPODType;
};

template <>
struct TypeTraits< unsigned int >
{
	typedef __TrueType     __IsPODType;
};

template <>
struct TypeTraits< long>
{
	typedef __TrueType     __IsPODType;
};

template <>
struct TypeTraits< unsigned long >
{
	typedef __TrueType     __IsPODType;
};

template <>
struct TypeTraits< long long >
{
	typedef __TrueType     __IsPODType;
};

template <>
struct TypeTraits< unsigned long long>
{
	typedef __TrueType     __IsPODType;
};

template <>
struct TypeTraits< float>
{
	typedef __TrueType     __IsPODType;
};

template <>
struct TypeTraits< double>
{
	typedef __TrueType     __IsPODType;
};

template <>
struct TypeTraits< long double >
{
	typedef __TrueType     __IsPODType;
};

template <class _Tp>
struct TypeTraits< _Tp*>
{
	typedef __TrueType     __IsPODType;
};


你可能感兴趣的:(函数,类型,;模板)