C++11 TypeList 妙用

源码展示:

#include 

using namespace std;


template  struct typelist;

typedef typelist <int ,short ,double ,long ,float> defaultPolicys;

template  struct concat;

template 
struct concat, typelist >
{
    typedef typelist type;
};

template
struct concat, T >
{
    typedef typelist type;
};

template
struct concat< T, typelist >
{
    typedef typelist  type;
};

template int I, typename K= defaultPolicys> struct replace;

template int I, typename H,typename ...Tail> struct replace>
{
    typedef typename concat1, typelist>::type>::type type;
};

template  struct replace0,typelist>
{
    typedef typelist type;
};

template int I> struct Custom
{
    const static int index = I;
    typedef T newType;
};



template  struct CEO;

template <> struct CEO<>
{
    typedef defaultPolicys myPolicys;
};

template  struct CEO
{
    typedef typename replace::type myPolicys;
};
//template  struct CEO{ typedef typename replace::myPolicys>::type myPolicy;};

template  struct CEO
{
    typedef typename replace::myPolicys>::type myPolicys;
};

int main()
{

    typedef typelist <int ,short ,double ,long ,float> five;
    typedef typelist <int ,short ,string, char ,string> fives;
    if(is_same::myPolicys,five>::value)cout<<"..."<<endl;
    if(is_same< CEO< Custom<string,2>,Custom<char,3>,Custom<string,4> > ::myPolicys,fives>::value)cout<<"..."<<endl;

    return 0;

}

 

template  struct typelist; typelist声明
template  struct concat; 连接任意类型至typelist头或尾部声明

template 
  struct concat, typelist >
  {
 typedef typelist type;  }; 连接两个typelist偏特化定义

template
struct concat, T >
{
    typedef typelist type;
}; 将类型连接至typelist尾部偏定义

template
struct concat< T, typelist >
{
    typedef typelist  type;
}; 将类型连接至typelist头部偏特化定义

template int I, typename K = defaultPolicys> struct replace; 用类型T替换K=typelist的I位置类型的声明

template int I, typename H,typename ...Tail> struct replace>
{
    typedef typename concat1, typelist>::type>::type type; }; 偏特化定义,递归入口 template  struct replace0,typelist> { typedef typelist type; }; 偏特化定义,递归出口
 
   
template int I> struct Custom
{
    const static int index = I; typedef T newType; }; 用于自定义类型
 
   
template  struct CEO; CEO:Policys的执行者

template <> struct CEO<> { typedef defaultPolicys myPolicys; CEO<>拥有默认的Policys }; template  struct CEO { typedef typename replace::type myPolicys; }; 自定义单个Policy的偏特化定义 //template  struct CEO{ typedef typename replace::myPolicys>::type myPolicy;};  template  struct CEO { typedef typename replace::myPolicys>::type myPolicys; }; 自定义多个Policys的偏特化定义

使用如下代码初步测试:
 CEO< Custom,Custom,Custom > ::myPolicys
myPolicys 类型为 typelist<int ,short ,string ,char ,string>
----------------------------------------------------------------------------------------------------------------------------------------
这段代码有什么用?
假设在typelist中,每一个类型拥有一个静态的函数,若如此CEO<>拥有一套默认的函数。
CEO> ,将替换掉某个默认的函数行为。
这听起来有点像模板方法模式,但我们使用是静多态,并没有使用继承和虚函数机制
而且用户使用也相当容易,并且代码更容易扩展,如果需要更改默认的Policys,只需扩充默认typelist即可。

在《C++ Template》 一书中,继承与模板那一章的第一节,讲述的是如何使用多继承和模板完成上述功能,而在《C++ 设计新思维》中讲到了typelist技术,而如今C++14提供可变长模板参数。
结合此三项,初步实现上述代码文章标题 言为妙用,实不敢当,有兴趣的同学,可以继续深入研究,在此抛砖引玉。

转载请表明出处,谢谢合作







转载于:https://www.cnblogs.com/tangzhenqiang/p/4209100.html

你可能感兴趣的:(c/c++)