关闭

Typelist介绍

148人阅读 评论(0) 收藏 举报

简单的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;
}
总结,递归,不同版本模仿处理


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:25245次
    • 积分:611
    • 等级:
    • 排名:千里之外
    • 原创:27篇
    • 转载:63篇
    • 译文:0篇
    • 评论:5条
    文章分类
    最新评论