• 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提供可变长模板参数。
    结合此三项,初步实现上述代码文章标题 言为妙用,实不敢当,有兴趣的同学,可以继续深入研究,在此抛砖引玉。

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







  • 相关阅读:
    scanf正则表达式
    找出 alter system kill session ‘sid,serial#’ kill 掉的数据库会话对应进程
    查询包含某个字段的表
    Oracle12C查询自建用户(非系统自带)
    监控慢SQL
    根据SPID查找SQL语句
    查看表空间使用率
    找出在使用临时表空间的SQL
    重设数据文件大小sql语句
    查看锁表进程SQL语句
  • 原文地址:https://www.cnblogs.com/tangzhenqiang/p/4209100.html
Copyright © 2020-2023  润新知