Typelist介绍

简单的Typelist
typedef Typelist <int ,NullType> OneTypeOnly;
typedef Typelist <char,Typelist<signed char,Typelist<unsigned char,NullType> > >AllCharTypes;
利用宏包装
typedef TYPELIST_4(signed char,short int,int ,long int)SignedIntegrals;
计算长度
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 Head,class Tail>
struct TypeAt<Typelist <Head,Tail>,0>
{
typedef Head Result; 
};
template <class Head ,class Tail ,unsigned int i >
struct TypeAt<Typelist<Head,Tail>,i>
{
typedef typename TypeAt<Tail , i-1>::Result Result;
};
查找Typelists
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};
};
添加Typelists
template<class TList,clsss T>struct Append;
template<>struct Append<NullType,NullType>
{
typedef NullType Result;
};
template<class T >struct Append<NullType,T>
{
typedef TypeLIST_l(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>
{
typedef Typelist<Head,typename Append<Tail ,T>::Result>
Result;
};
删除
template<class TList,class T> struct Erase;
template<class T>
//结束位置 
struct Erase<NullType,T>
{
typedef NullType Result;
};
//递归
template<class Tail ,class T>
struct Erase{Typelist<T,Tail>,T}
{
typedef Tail Result;
}
//入口
template<class Head,class Tail ,class T>
struct Erase<Typelist<Head,Tail>,T>
{
typedef Typelist<Head,typename Erase<Tail ,T>::Result>
Result;
};
删除所有指定的型别
template<class TList,class T> struct EraseAll;
template<class T>
//结束位置 
struct EraseAll<NullType,T>
{
typedef NullType Result;
};
//递归
template<class Tail ,class T>
struct EraseAll{Typelist<T,Tail>,T}
{
//typedef Tail Result;
typedef typename EraseAll<Tail ,T>:: Result Result
}
//入口
template<class Head,class Tail ,class T>
struct Erase<Typelist<Head,Tail>,T>
{
typedef Typelist<Head,typename Erase<Tail ,T>::Result>
Result;
};
移除所有重复元素
template<class TList> struct NoDuplicates;
template<>struct NoDuplicates<NullType>
{
typedef NullType Result;
}
template<class Head,class Tail>
struct NoDuplicates<Typelist<Head,Tail> >
{
private:
typedef typename NoDuplicates<Tail>::Result L1;
typedef typename Erase<L1,Head>::Result L2;
public:
typedef Typelist<Head,L2>Result;
};
替换元素
template<class TList,class T,class U> struct Replace;
template<class T,class U>
struct Replace<NullType,T,U>
{
typedef NullType Result;
};
template<class T,class Tail ,class U>
struct Replace <Typelist<T,Tail>,T,U>
{
typedef Typelist<U,Tail >Result;
};
template<class Head,class Tail ,class T,class U>
struct Replace <Typelist<Head,Tail>,T,U>
{
typedef Typelist<Head,typename Replace <Tail ,T,U>::Result>
Result;
};

template <class TList ,class T> struct MostDerived
 
//排序功能,关系公式SUPERSUBCLASS
template< class T >
struct MostDerived <NullType ,T>
{
 typedef Result;
};

template <class Head,class Tail,class T>
struct MostDerived <Typelist<Head,Tail>,T>
{
 private:
 typedef typename MostDerived<Tail ,T >::Result Candidate;
 public:
  typedef  typename Select <SUPERSUBCLASS(Candidate,Head),Head,Candidate>::Result Result;
}

template<class T >struct DerivedToFront;
template<>
struct DerivedToFront<NullType>
{
 typedef NullType Result;
};
template <class Head ,class Tail >
struct DerivedToFront<Typelist<Head,Tail> >
{
 private:
 typedef typename MostDerived <Tail ,Head>::Result TheMostDerived
 typedef typename Replace<Tail ,TheMostDerived ,Head>::Result L;
 public :
 typedef Typelist<TheMostDerived ,L>Result;
}
总结,递归,不同版本模仿处理


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shenya1314/article/details/53995469
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

Typelist介绍

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭