自己编写的TypeListDemo,不错很好玩的东西
#include <iostream> #include <cassert> #include <cstring> using namespace std; struct NullType {}; template<class Head, class Tail> struct TypeList { typedef Head HeadType; typedef Tail TailType; }; //仅仅声明,不实现 template<class TList> struct Length; template<> struct Length<NullType> { static enum {value = 0,}; }; template<class Head, class Tail > struct Length<TypeList<Head, Tail> > { static enum { value = 1 + Length<Tail>::value, }; }; template<class, unsigned int> struct TypeAt; template<class T, class U> struct TypeAt<TypeList<T, U> , 0> { typedef T ResultType; }; template<unsigned int index> struct TypeAt<NullType, index> { typedef NullType ResultType; }; template<class T, class U, unsigned int index> struct TypeAt<TypeList<T, U>, index> { typedef typename TypeAt<U, index -1>::ResultType ResultType; }; template <class, class> struct IndexOf; template<class Head, class Tail> struct IndexOf<TypeList<Head, Tail>, NullType> { static enum {value = -1,}; }; template<class Head, class Tail> struct IndexOf<TypeList<Head, Tail>, Head> { static enum{value = 0,}; }; template<class T> struct IndexOf<NullType, T> { static enum {value = -1,}; }; template<class Head, class Tail, class T> struct IndexOf<TypeList<Head, Tail>, T> { static enum {tmpvalue = IndexOf<Tail, T>::value ,}; static enum {value = (tmpvalue== -1) ? -1:(tmpvalue+1),}; }; template<class, class> struct Append; template<class Head, class Tail> struct Append<TypeList<Head, Tail>, NullType> { typedef NullType ResultType; }; template<class T> struct Append<NullType, T> { typedef TypeList<T, NullType> ResultType; }; template<class Head, class Tail, class T> struct Append<TypeList<Head, Tail>, T> { typedef TypeList<Head, typename Append<Tail, T>::ResultType> ResultType; }; template<class, class> struct Erase; template<class Head, class Tail> struct Erase<TypeList<Head, Tail>, NullType> { typedef TypeList<Head, Tail> ResultType; }; template<class T> struct Erase<NullType, T> { typedef NullType ResultType; }; template<class Head, class Tail> struct Erase<TypeList<Head, Tail>, Head> { typedef Tail ResultType; }; template<class Head, class Tail, class T> struct Erase<TypeList<Head, Tail>, T> { typedef typename Erase<Tail, T>::ResultType ResultType; }; template<class T, class U> struct IsSame { static enum {value = false,}; }; template<class T> struct IsSame<T, T> { static enum {value = true,}; }; void length_test() { typedef TypeList<int, TypeList<double, TypeList<char, NullType>>> ThreeTypeList; assert(Length<ThreeTypeList>::value == 3); typedef TypeAt<ThreeTypeList, 0>::ResultType IntType; typedef TypeAt<ThreeTypeList, 1>::ResultType DoubleType; int s1 = IsSame<TypeAt<ThreeTypeList, 10000>::ResultType , NullType>::value; bool b1 = std::is_same<int, IntType>::value ; bool b2 = std::is_same<double, DoubleType>::value; assert((IndexOf<ThreeTypeList, int>::value == 0)); assert((IndexOf<ThreeTypeList,double>::value == 1)); assert((IndexOf<ThreeTypeList, char>::value == 2)); assert((IndexOf<ThreeTypeList, NullType>::value == -1)); assert((IndexOf<ThreeTypeList, std::string>::value == -1)); typedef Append<ThreeTypeList, double>::ResultType FourTypeList; int len = Length<FourTypeList>::value; int v1 = IsSame<TypeAt<FourTypeList, 0>::ResultType, int>::value; int v2 = IsSame<TypeAt<FourTypeList, 1>::ResultType, double>::value; int v3 = IsSame<TypeAt<FourTypeList, 2>::ResultType, char>::value; int v4 = IsSame<TypeAt<FourTypeList, 3>::ResultType, double>::value; typedef Erase<FourTypeList, int>::ResultType DCDType; int e1 = IsSame<TypeAt<DCDType, 0>::ResultType, double>::value; int e2 = IsSame<TypeAt<DCDType, 1>::ResultType, char>::value; int e3 = IsSame<TypeAt<DCDType, 2>::ResultType, double>::value; std::cout<<typeid(DoubleType).name()<<std::endl; int aaa = 0; } int _tmain(int argc, _TCHAR* argv[]) { length_test(); char c = getchar(); return 0; }