TypeList IndexOf

#include <cstdlib>
#include <iostream>
#include <typeinfo>
#include <string>
using namespace std;
class NullType;
namespace TL
{
  template<class T,class U>
  struct TypeList
  {
    typedef T head;
    typedef U tail;
  };
}
using namespace TL;
#define TYPELIST_1(T1) TypeList<T1,NullType>
#define TYPELIST_2(T1,T2) TypeList<T1,TYPELIST_1(T2)>
#define TYPELIST_3(T1,T2,T3) TypeList<T1,TYPELIST_2(T2,T3)>
typedef TypeList<char,TypeList<signed char,unsigned char> > CharList;
template<class Tlist>struct Length;
template<>struct Length<NullType>
{
  enum{value = 0};
};
template<class T,class U>
struct Length<TypeList<T,U> >
{
  enum{value = 1 + Length<U>::value};
};
//---------------------------------------------------------------
//利用索引查找对象
template<class T,int U>struct TypeAt;
template<class head,class tail>
struct TypeAt<TypeList<head,tail>,0>
{
  typedef head Result;
};
template<class head,class tail,int i>
struct TypeAt<TypeList<head,tail>,i>
{
  typedef typename TypeAt<tail,i - 1>::Result Result;
};
//---------------------------------------------------------------
//indexof
template<class TList,class T>struct IndexOf;
template<class T>
struct IndexOf<NullType,T>
{
  enum{value = -1};
};
template<class Tail,class T>
struct IndexOf<TypeList<T,Tail>,T>
{
  enum{value = 0};
};
template<class Head,class Tail,class T>
struct IndexOf<TypeList<Head,Tail>,T>
{
private:
  enum{temp = IndexOf<Tail,T>::value};
public:
  enum{value = temp == -1 ? -1 : 1 + temp};
};
int main(int argc, char *argv[])
{
    typedef TYPELIST_3(char,int,string) MyTypeList;
    cout<<"IndexOf-----------------------------------------"<<endl;
    cout<<"NullType的索引值:"<<IndexOf<MyTypeList,NullType>::value<<endl;
    cout<<"string的索引值:"<<IndexOf<MyTypeList,string>::value<<endl;
    cout<<"int的索引值:"<<IndexOf<MyTypeList,int>::value<<endl;
    cout<<"char的索引值:"<<IndexOf<MyTypeList,char>::value<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

你可能感兴趣的:(String,struct,System,Class,include)