Type Traits

编译期根据型别作判断的泛型技术

template

class TypeTraits

{

private :

template struct PointerTraits

{

enum{result = false};

typedef NullType PointeeType;

}

template struct PointerTraits

{

enum {

result =true;

typedef U PointeeType;

}

public:

enum{isPointer = PointerTraits::result};

typedef PointerTraits::PointeeType PointeeType;

}

}
如下std::vector::iterator实做内容

int main()

{

const bool

iterIsPtr = TypeTraits::itorator>::isPointer;

cout <<"vector::iterator is"<

}

侦测Pointers to members

template

class TypeTraits

{

private:

templatestruct PToMraits

{

enum {result=false;}

}

template

struct PToMTraits

{

enum{result = true};

}

public :

enum{

isMemberPointer = PToMTraits::result};

}

}

侦测基本型别方法 

IndexOf

优化参数型别 

referencedType 和 isPrimitive

卸除饰词const与Volatile

template

class TypeTraits

{

private:

templatestruct UnConst

{

typedef U Result;

}

template struct UnConst

{

typedef U result;

}

public :

typedef UnConst::Result NonConstType;

}

总结:基本结合了C++ 与 template ,通过Int2Type<>方式,实现了如上许多的功能,其主要作用在编译期实现功能,语法难度更深层次。



你可能感兴趣的:(template)