zoukankan      html  css  js  c++  java
  • C++11 TypeList 妙用

    源码展示:

    #include <iostream>
    
    using namespace std;
    
    
    template <typename ... Args> struct typelist;
    
    typedef typelist <int ,short ,double ,long ,float> defaultPolicys;
    
    template <typename A, typename B> struct concat;
    
    template <typename... A, typename... B>
    struct concat<typelist<A...>, typelist<B...> >
    {
        typedef typelist<A..., B...> type;
    };
    
    template<typename T, typename... TList>
    struct concat<typelist<TList...>, T >
    {
        typedef typelist<TList..., T> type;
    };
    
    template<typename T, typename... TList>
    struct concat< T, typelist<TList...> >
    {
        typedef typelist<T, TList...>  type;
    };
    
    template <typename T ,int I, typename K= defaultPolicys> struct replace;
    
    template <typename T, int I, typename H,typename ...Tail> struct replace<T,I,typelist<H,Tail...>>
    {
        typedef typename concat<H, typename replace<T, I-1, typelist<Tail...>>::type>::type type;
    };
    
    template <typename T,typename H,typename... Tail> struct replace<T,0,typelist<H,Tail...>>
    {
        typedef typelist<T,Tail...> type;
    };
    
    template <typename T ,int I> struct Custom
    {
        const static int index = I;
        typedef T newType;
    };
    
    
    
    template <typename ...> struct CEO;
    
    template <> struct CEO<>
    {
        typedef defaultPolicys myPolicys;
    };
    
    template <typename T> struct CEO<T>
    {
        typedef typename replace<typename T::newType,T::index>::type myPolicys;
    };
    //template <typename T,typename U> struct CEO<T,U>{ typedef typename replace<typename U::newType,U::index,typename CEO<T>::myPolicys>::type myPolicy;};
    
    template <typename T,typename ... Tail> struct CEO<T,Tail...>
    {
        typedef typename replace<typename T::newType,T::index,typename CEO<Tail...>::myPolicys>::type myPolicys;
    };
    
    int main()
    {
    
        typedef typelist <int ,short ,double ,long ,float> five;
        typedef typelist <int ,short ,string, char ,string> fives;
        if(is_same<typename CEO<>::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 <typename ... Args> struct typelist; typelist声明
    template <typename A, typename B> struct concat; 连接任意类型至typelist头或尾部声明

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

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

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

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

    template <typename T, int I, typename H,typename ...Tail> struct replace<T,I,typelist<H,Tail...>>
    {
        typedef typename concat<H, typename replace<T, I-1, typelist<Tail...>>::type>::type type;
    }; 偏特化定义,递归入口
    
    template <typename T,typename H,typename... Tail> struct replace<T,0,typelist<H,Tail...>>
    {
        typedef typelist<T,Tail...> type;
    }; 偏特化定义,递归出口
    
    
    template <typename T ,int I> struct Custom
    {
        const static int index = I;
        typedef T newType;
    }; 用于自定义类型
    
    
    template <typename ...> struct CEO; CEO:Policys的执行者
    
    template <> struct CEO<>
    {
        typedef defaultPolicys myPolicys; CEO<>拥有默认的Policys
    };
    
    template <typename T> struct CEO<T>
    {
        typedef typename replace<typename T::newType,T::index>::type myPolicys;
    }; 自定义单个Policy的偏特化定义
    //template <typename T,typename U> struct CEO<T,U>{ typedef typename replace<typename U::newType,U::index,typename CEO<T>::myPolicys>::type myPolicy;};
    
    template <typename T,typename ... Tail> struct CEO<T,Tail...>
    {
        typedef typename replace<typename T::newType,T::index,typename CEO<Tail...>::myPolicys>::type myPolicys;
    }; 自定义多个Policys的偏特化定义

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

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

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







  • 相关阅读:
    Python自动化交易学习笔记(九)——将添加佣金数据,来使回测更逼近与真实交易。
    python自动化交易学习笔记五-Demo8-策略已经设置了买入条件-设置卖出条件
    python自动化交易学习笔记(四)第一个策略回测程序Demo7(添加买入条件-当股价连续下跌2天后买入)
    python量化交易学习笔记(三)——第一个策略回测程序Demo6
    取sql server 图片二进制流
    JDBC连接sql取数据
    量化交易学习笔记(二)-实例化Cerebro引擎-代理已经手握一笔资金准备进行交易
    maven本地化jbarcode-0.2.8.jar
    量化自动化交易python学习笔记之(一)BaoStock使用A股K线数据股票代码sh.60000,四年历史数据,用于后期追溯测试和策略可行性
    Angularjs插件ui-select的使用方法
  • 原文地址:https://www.cnblogs.com/tangzhenqiang/p/4209100.html
Copyright © 2011-2022 走看看