SOURCE Display:
#include <iostream>using namespacestd;Template<typename ... Args>structtypelist;typedef typelist<int, Short,Double,Long,float>defaultpolicys;template<typename A, TypeName b>structconcat;template<typename ... A, TypeName ... B>structConcat<typelist<a...>, typelist<b...> >{typedef typelist<a ..., b...>.type;}; Template<typename T, TypeName ... Tlist>structConcat<typelist<tlist...>, T >{typedef typelist<tlist ..., t>.type;}; Template<typename T, TypeName ... Tlist>structconcat< T, typelist<tlist...> >{typedef typelist<t, tlist...>type;}; Template<typename T,intI, TypeName k= defaultpolicys>structreplace;template<typename T,intI, TypeName H,typename ... Tail>structReplace<t,i,typelist{typedef typename CONCAT1, typelist<tail...>>::type>:: type type;}; Template<typename t,typename H,typename ... Tail>structReplace<t,0,typelist{typedef typelist<T,Tail...>type;}; Template<typename T,intI>structcustom{Const Static intindex =I; typedef T NewType;}; Template<typename ...>structceo;template<>structCeo<>{typedef defaultpolicys MYPOLICYS;}; Template<typename t>structCeo<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>structCeo<t,tail...>{typedef typename Replace<typename T::newtype,t::index,typename ceo<tail...>::mypolicys>:: type Mypolicys;};intMain () {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;}
structtypelist Declaration
struct concat; Connect any type to Typelist header or tail declaration
Template <typename ... A, TypeName ... B> struct concat<typelist<a...>, typelist<b...> > { type; "; connection two typelist the definition of
Template<typename T, TypeName ... Tlist>
struct concat<typelist<tlist...>, T;
{
typedef typelist<tlist ..., t> type;
}; Connect the type to the typelist tail-offset definition
Template<typename T, TypeName ... Tlist>
struct concat< T, typelist<tlist...>;
{
typedef typelist<t, tlist...> type;
}; < Span style= "color: #008080;" > connect type to Typelist header biasing definition
template <typename T, int I, typename K = defaultpolicys> struct replace; type T replaces the declaration of the I position type of k=typelist
struct replace<t,i,typelist{ typedef typename CONCAT<H, TypeName replace<t, I- 1, typelist<tail...>>::type>struct replace<t,0,typelist{ typedef typelist<t,tail...> type;}; The definition of partial specificity, the export of recursion
Template <typename T,struct custom{ int index = I; typedef T NewType;}; for custom type
Template <typename ...>structCEO; Ceo:policys's performer template <>struct ceo<>{typedef defaultpolicys MYPOLICYS; Ceo<> has the default policys};template <typename t> struct Ceo<t>{typedef typename Replace<typename T::newtype,t::index>::type Mypolicys;}; Customizing the customization definition for a single 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;}; Customizing the POLICYS definition of multiple
Use the following code to initially test:
ceo< custom<string,2>,custom<char,3>,custom<string,4> >:: Mypolicys
Mypolicys type typelist<int, short, String , char , string>
----------------------------------------------------------------------------------------------------------- -----------------------------
What's the use of this piece of code?
Suppose that in typelist, each type has a static function, so ceo<> has a set of default functions.
ceo<custom<xtype,xindex>>, a default function behavior is replaced.
This sounds a bit like template method pattern , but we use static polymorphism and do not use inheritance and virtual function mechanisms。
And the user is fairly easy to use, and the code is much easier to extend, and if you need to change the default Policys, just expand the default typelist.
In the book "C + + template", the first section of the chapter on inheritance and templates is about how to use multiple inheritance and templates to accomplish these functions, whereas in "C + + Design new thinking" The Typelist technology is described, and now C++14 provides variable-length template parameters.
Combining these three items, the initial implementation of the above code。The title of the article is magical, real deserve, interested students, can continue to further research, here.
Reprint Please indicate the source, thank you for your cooperation
C++11 typelist Magical