C++11 TypeList ????
???????????? ???????[ 2015/1/27 17:50:55 ] ????????C++ net
?????????:
#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?????????????
??????????????????????????????±??? ???????????????????????????????????????о?????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11