C++11 TypeList 妙用

本文介绍了一种利用C++模板元编程实现策略模式的方法,通过定义类型列表和替换策略来改变默认的行为,实现了类似模板方法的设计模式,但完全基于静态多态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

源码展示:

<span style="font-size:14px;">#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 ... 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;

}</span>


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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值