C++的模板元编程非常强大呀,这里编写了一个类型链表,并提供了对该类型链表的:插入,删除,查找,排序,反转 等操作。 有兴趣的可以研究一下!
#include <cstdlib>
namespace shediao{
struct NullType;
template <typename H, typename T>
struct TypeList{
typedef H Head;
typedef T Tail;
};
template
<
typename T1 = NullType, typename T2 = NullType, typename T3 = NullType, typename T4 = NullType,
typename T5 = NullType, typename T6 = NullType, typename T7 = NullType, typename T8 = NullType,
typename T9 = NullType, typename T10 = NullType, typename T11 = NullType, typename T12 = NullType,
typename T13 = NullType, typename T14 = NullType, typename T15 = NullType, typename T16 = NullType,
typename T17 = NullType, typename T18 = NullType, typename T19 = NullType, typename T20 = NullType,
typename T21 = NullType, typename T22 = NullType, typename T23 = NullType, typename T24 = NullType,
typename T25 = NullType, typename T26 = NullType, typename T27 = NullType, typename T28 = NullType,
typename T29 = NullType, typename T30 = NullType, typename T31 = NullType, typename T32 = NullType
>
struct MakeTypelist
{
private:
typedef typename MakeTypelist < T2 , T3 , T4 , T5 , T6 , T7 , T8 , T9 , T10, T11,
T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, T23, T24, T25,
T26, T27, T28, T29, T30, T31, T32
>::Result TailResult;
public:
typedef TypeList<T1, TailResult> Result;
};
template<>
struct MakeTypelist<>
{
typedef NullType Result;
};
template<typename T>
struct Length;
template<>
struct Length<NullType>{
static const int value = 0;
};
template<typename T,typename H>
struct Length<TypeList<H,T> >{
static const int value = 1 + Length<T>::value;
};
template <typename TypeList,typename Type>
struct Append;
template <>
struct Append<NullType, NullType>{
typedef NullType Result;
};
template <typename Type>
struct Append<NullType,Type>{
typedef TypeList<Type,NullType> Result;
};
template <typename H, typename T>
struct Append<TypeList<H,T>, NullType>{
typedef TypeList<H,T> Result;
};
template <typename H, typename T ,typename Type>
struct Append<TypeList<H,T>,Type >{
typedef TypeList<H,typename Append<T,Type>::Result> Result;
};
template <typename TypeList,typename Type>
struct PushBack{
typedef Append<TypeList,Type>::Result;
};
template <typename,typename>
struct PushFront;
template <typename H,typename T>
struct PushFront<TypeList<H,T>,NullType>{
typedef TypeList<H,T> Result;
};
template <typename H,typename T,typename PT>
struct PushFront<TypeList<H,T>,PT>{
typedef TypeList<PT,TypeList<H,T> > Result;
};
template <typename,unsigned int,typename>
struct Insert;
template </*typename H,typename T*/typename Typelist,typename IT>
struct Insert<Typelist,0,IT>{
typedef TypeList<IT,Typelist> Result;
};
template <typename H,typename T,unsigned int N,typename IT>
struct Insert<TypeList<H,T>,N,IT>{//这里当N为负数的情况没有考虑
typedef TypeList<H,typename Insert<T,N-1,IT>::Result> Result;
};
template <typename, unsigned int>
struct Delete;
template <unsigned int N>
struct Delete<NullType,N>{
typedef NullType Result;
};
template <typename T,typename H>
struct Delete<TypeList<H,T>,0>{
typedef T Result;
};
template <typename H,typename T,unsigned int N>
struct Delete<TypeList<H,T>,N>{
typedef TypeList<H,typename Delete<T,N-1>::Result> Result;
};
template <typename,typename>
struct Erase;
template <typename H,typename T,typename ET>
struct Erase<TypeList<H,T>, ET>{
typedef typename TypeList<H,typename Erase<T,ET>::Result> Result;
};
template <typename H,typename T>
struct Erase<TypeList<H,T>, H>{
typedef T Result;
};
template <typename T,typename H,template<typename,typename> class Typelist>
struct Erase<Typelist<H,T>, NullType>{
typedef Typelist<H,T> Result;
};
template <typename,typename>
struct EraseAll;
template <typename H,typename T,typename ET>
struct EraseAll<TypeList<H,T>, ET>{
typedef typename TypeList<H,typename EraseAll<T,ET>::Result> Result;
};
template <typename H,typename T>
struct EraseAll<TypeList<H,T>, H>{
typedef typename EraseAll<T,H>::Result Result;
};
template <typename T,typename H,template<typename,typename> class Typelist>
struct EraseAll<Typelist<H,T>, NullType>{
typedef Typelist<H,T> Result;
};
template <typename>
struct Reverse;
template <>
struct Reverse<NullType>{
typedef NullType Result;
};
template <typename H, typename T>
struct Reverse<TypeList<H,T> >{
typedef typename Append<typename Reverse<T>::Result,H>::Result Result;
};
template <typename,typename>
struct IndexOf;
template <typename T>
struct IndexOf<NullType,T>{
static const int value = -1;
};
template <typename H,typename T>
struct IndexOf<TypeList<H,T>,H>{
static const int value = 0;
};
template <typename H,typename T,typename H2>
struct IndexOf<TypeList<H,T>,H2>{
static const int value = IndexOf<T,H2>::value == -1 ? -1 : 1 + IndexOf<T,H2>::value;//这里IndexOf多算了一次
};
template <typename ,unsigned int, typename = NullType>
struct TypeAt;
template <unsigned int N, typename DefaultType>
struct TypeAt<NullType,N,DefaultType>{
typedef DefaultType Result;
};
template <typename H,typename T,typename DefaultType>
struct TypeAt<TypeList<H,T>,0,DefaultType>{
typedef H Result;
};
template <typename H,typename T,unsigned int N,typename DefaultType>
struct TypeAt<TypeList<H,T>,N,DefaultType>{
typedef typename TypeAt<T,N-1>::Result Result;
};
template <typename>
struct Unique;
template <>
struct Unique<NullType>{
typedef NullType Result;
};
template <typename H,typename T>
struct Unique<TypeList<H,T> >{
typedef TypeList<H,typename Unique<typename EraseAll<T,H>::Result>::Result> Result;
};
template <typename,typename,typename>
struct Replace;
template <typename SrcType,typename RepType>
struct Replace<NullType, SrcType, RepType>{
typename NullType Result;
};
template <typename H,typename T,typename SrcType,typename RepType>
struct Replace<TypeList<H,T>, SrcType, RepType>{
typename TypeList<H, typename Replace<T,SrcType,RepType>::Result> Result;
};
template <typename H,typename T,typename RepType>
struct Replace<TypeList<H,T>, H/*SrcType*/, RepType>{
typename TypeList<RepType, T> Result;
};
template <typename,typename,typename>
struct ReplaceAll;
template <typename SrcType,typename RepType>
struct ReplaceAll<NullType, SrcType, RepType>{
typename NullType Result;
};
template <typename H,typename T,typename SrcType,typename RepType>
struct ReplaceAll<TypeList<H,T>, SrcType, RepType>{
typename TypeList<H, typename ReplaceAll<T,SrcType,RepType>::Result> Result;
};
template <typename H,typename T, typename RepType>
struct ReplaceAll<TypeList<H,T>, H/*SrcType*/, RepType>{
typename TypeList<RepType, typename ReplaceAll<T,H,RepType>::Result> Result;
};
template <bool,typename,typename>
struct Select;
template <typename IfType,typename ElseType>
struct Select<true,IfType,ElseType>{
typedef IfType Result;
};
template <typename IfType,typename ElseType>
struct Select<false,IfType,ElseType>{
typedef ElseType Result;
};
template <typename T>
struct TypeSize{
static const int value = sizeof(T);
};
template <>
struct TypeSize<NullType>{
static const int value = -1;
};
template <typename T1,typename T2,template<typename> class TypeValue= TypeSize>
struct Compare{
static const int value = ((TypeValue<T1>::value > TypeValue<T2>::value) ? 1 : ((TypeValue<T2>::value > TypeValue<T1>::value) ? -1 : 0));
};
template <typename,template <typename, typename, template<typename> class > class = Compare>
struct Max;
template <template<typename,typename,template<typename>class>class Comp>
struct Max<NullType,Comp>{
static const int value = -1;
};
template <typename H,typename T,/*template<typename,typename,template<typename>class>class*/typename Comp>
struct Max<TypeList<H,T>,Comp >{
static const int value = (Max<T,Comp>::value == -1 || Comp<H,typename TypeAt<T,Max<T,Comp>::value>::Result>::value >= 0) ? 0 : 1 + Max<T,Comp>::value;
};
template <typename,template <typename, typename, template<typename> class > class Comp = Compare>
struct Min;
template <typename T>
struct Min<NullType,T>{
static const int value = -1;
};
template <typename H,typename T,/*template <typename, typename, template<typename> class > class*/typename Comp>
struct Min<TypeList<H,T> ,Comp>{
static const int value = (Min<T,Comp>::value == -1 || Comp<H,typename TypeAt<T,Min<T,Comp>::value>::Result>::value <= 0) ? 0 : 1 + Min<T,Comp>::value;
};
template <typename,template <typename,template <typename, typename, template<typename> class > class > class = Min>
struct Sort;
template <typename T>
struct Sort<NullType,T>{
typedef NullType Result;
};
template <typename H,typename T,template<typename>class OperFun>
struct Sort<TypeList<H,T>,OperFun>{
typedef typename Select<
Min<
TypeList<H,T>
>::value == 0,
TypeList<H,typename Sort<T,Min>::Result>,
TypeList<typename TypeAt< TypeList<H,T>, Min<TypeList<H,T> >::value >::Result,
typename Sort<typename Delete<TypeList<H,T>,Min<TypeList<H,T> >::value>::Result,Min>::Result>
>::Result Result;
};
template <typename,typename>
struct Foreach;
}
#define MTL1(T1) shediao::TypeList<T1, shediao::NullType>
#define MTL2(T1, T2) \
shediao::TypeList<T1, MTL1(T2)>
#define MTL3(T1, T2, T3) \
shediao::TypeList<T1, MTL2(T2, T3)>
#define MTL4(T1, T2, T3, T4) \
shediao::TypeList<T1, MTL3(T2, T3, T4)>
#define MTL5(T1, T2, T3, T4, T5) \
shediao::TypeList<T1, MTL4(T2, T3, T4, T5)>
#define MTL6(T1, T2, T3, T4, T5, T6) \
shediao::TypeList<T1, MTL5(T2, T3, T4, T5, T6)>
#define MTL7(T1, T2, T3, T4, T5, T6, T7) \
shediao::TypeList<T1, MTL6(T2, T3, T4, T5, T6, T7)>
#define MTL8(T1, T2, T3, T4, T5, T6, T7, T8) \
shediao::TypeList<T1, MTL7(T2, T3, T4, T5, T6, T7, T8)>
#define MTL9(T1, T2, T3, T4, T5, T6, T7, T8, T9) \
shediao::TypeList<T1, MTL8(T2, T3, T4, T5, T6, T7, T8, T9)>
#define MTL10(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) \
shediao::TypeList<T1, MTL9(T2, T3, T4, T5, T6, T7, T8, T9, T10)>
#define MTL11(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11) shediao::TypeList<T1, \
MTL10(T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)>
#define MTL12(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12) \
shediao::TypeList<T1, \
MTL11(T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)>
#define MTL13(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13) \
shediao::TypeList<T1, \
MTL12(T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13)>
#define MTL14(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14) \
shediao::TypeList<T1, \
MTL13(T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14)>
#define MTL15(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15) \
shediao::TypeList<T1, \
MTL14(T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15)>
#define MTL16(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16) \
shediao::TypeList<T1, \
MTL15(T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16)>
#define MTL17(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17) \
shediao::TypeList<T1, \
MTL16(T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17)>
#define MTL18(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18) \
shediao::TypeList<T1, \
MTL17(T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18)>
#define MTL19(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18, T19) \
shediao::TypeList<T1, \
MTL18(T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18, T19)>
#define MTL20(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18, T19, T20) \
shediao::TypeList<T1, \
MTL19(T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18, T19, T20)>
#define MTL21(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21) \
shediao::TypeList<T1, \
MTL20(T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21)>
#define MTL22(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, \
T21, T22) \
shediao::TypeList<T1, \
MTL21(T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, \
T21, T22)>
#define MTL23(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, \
T21, T22, T23) \
shediao::TypeList<T1, \
MTL22(T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, \
T21, T22, T23)>
#define MTL24(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, \
T21, T22, T23, T24) \
shediao::TypeList<T1, \
MTL23(T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, \
T21, T22, T23, T24)>
#define MTL25(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, \
T21, T22, T23, T24, T25) \
shediao::TypeList<T1, \
MTL24(T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, \
T21, T22, T23, T24, T25)>
#define MTL26(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, \
T21, T22, T23, T24, T25, T26) \
shediao::TypeList<T1, \
MTL25(T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, \
T21, T22, T23, T24, T25, T26)>
#define MTL27(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, \
T21, T22, T23, T24, T25, T26, T27) \
shediao::TypeList<T1, \
MTL26(T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, \
T21, T22, T23, T24, T25, T26, T27)>
#define MTL28(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, \
T21, T22, T23, T24, T25, T26, T27, T28) \
shediao::TypeList<T1, \
MTL27(T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, \
T21, T22, T23, T24, T25, T26, T27, T28)>
#define MTL29(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, \
T21, T22, T23, T24, T25, T26, T27, T28, T29) \
shediao::TypeList<T1, \
MTL28(T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, \
T21, T22, T23, T24, T25, T26, T27, T28, T29)>
#define MTL30(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, \
T21, T22, T23, T24, T25, T26, T27, T28, T29, T30) \
shediao::TypeList<T1, \
MTL29(T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, \
T21, T22, T23, T24, T25, T26, T27, T28, T29, T30)>
#define MTL31(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, \
T22, T23, T24, T25, T26, T27, T28, T29, T30, T31) \
shediao::TypeList<T1, \
MTL30(T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, \
T21, T22, T23, T24, T25, T26, T27, T28, T29, T30, T31)>
#define MTL32(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, \
T22, T23, T24, T25, T26, T27, T28, T29, T30, T31, T32) \
shediao::TypeList<T1, \
MTL31(T2, T3, T4, T5, T6, T7, T8, T9, T10, \
T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, \
T21, T22, T23, T24, T25, T26, T27, T28, T29, T30, \
T31, T32)>