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提供可變長模板參數。
結合此三項,初步實現上述代碼文章標題 言為妙用,實不敢當,有興趣的同學,可以繼續深入研究,在此拋磚引玉。

轉載請表明出處,謝謝合作








免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM