TypeList 之 Append

#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)>
#define TYPELIST_4(T1,T2,T3,T4) TypeList<T1,TYPELIST_3(T2,T3,T4)>
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};
};
//---------------------------------------------------------------
//Append
template<class TList,class T>struct Append;
template<>
struct Append<NullType,NullType>
{
  typedef NullType Result;
};
template<class T>
struct Append<NullType,T>
{
  typedef TYPELIST_1(T) Result;
};
template<class head,class Tail>
struct Append<NullType,TypeList<head,Tail> >
{
  typedef TypeList<head,Tail> Result;
};
template<class head,class Tail,class T>
struct Append<TypeList<head,Tail>,T>
{
  typedef TypeList<head,typename Append<Tail,T>::Result> Result;
};
int main(int argc, char *argv[])
{
    cout<<"Append-----------------------------------------"<<endl;
    typedef TYPELIST_4(char,signed char,unsigned char,string) CharList;
    typedef TYPELIST_3(int,float,double) IntList;
    typedef Append<NullType,int> Test1;
    cout<<"Append<NullType,int> Test1的长度:"<<Length<Test1::Result>::value<<endl;
    typedef Append<NullType,IntList> Test2;
    cout<<"typedef Append<NullType,IntList> Test2的长度:"<<Length<Test2::Result>::value<<endl;
    typedef Append<CharList,IntList> Test3;
    cout<<"typedef Append<CharList,IntList> Test3的长度:"<<Length<Test3::Result>::value<<endl;
    cout<<"-----------------------------------------"<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值