#include #include using namespace std; class NullType; namespace TL { template struct TypeList { typedef T head; typedef U tail; }; #define TYPELIST_1(T1) TypeList #define TYPELIST_2(T1,T2) TypeList #define TYPELIST_3(T1,T2,T3) TypeList #define TYPELIST_4(T1,T2,T3,T4) TypeList //----------------------------------------------------------------- //Length templatestruct Length; template<>struct Length { enum{value = 0}; }; template struct Length > { enum{value = 1 + Length::value}; }; //--------------------------------------------------------------- //利用索引查找对象 templatestruct TypeAt; template struct TypeAt,0> { typedef head Result; }; template struct TypeAt,i> { typedef typename TypeAt::Result Result; }; //--------------------------------------------------------------- //indexof templatestruct IndexOf; template struct IndexOf { enum{value = -1}; }; template struct IndexOf,T> { enum{value = 0}; }; template struct IndexOf,T> { private: enum{temp = IndexOf::value}; public: enum{value = temp == -1 ? -1 : 1 + temp}; }; //--------------------------------------------------------------- //Append templatestruct Append; template<> struct Append { typedef NullType Result; }; template struct Append { typedef TYPELIST_1(T) Result; }; template struct Append > { typedef TypeList Result; }; template struct Append,T> { typedef TypeList::Result> Result; }; //--------------------------------------------------------------- //Erase templatestruct Erase; template struct Erase { typedef NullType Result; }; template struct Erase,T> { typedef tail Result; }; template struct Erase,T> { typedef TypeList::Result> Result; }; //--------------------------------------------------------------- //NoDuplicate templatestruct NoDuplicate; template<>struct NoDuplicate { typedef NullType Result; }; template struct NoDuplicate > { private: typedef typename NoDuplicate::Result L1; typedef typename Erase::Result L2; public: typedef TypeList Result; }; } using namespace TL; int main(int argc, char *argv[]) { typedef TYPELIST_4(int,float,char,double) Test1; cout<<"Erase 前的深度为:"<::value<::Result Test2; cout<<"Erase 后的深度为:"<::value<