模板的编译错误,错在哪里?
从<<Modern C++ design>>里面摘出来的内容:
#include "stdafx.h"// compile time programmingstruct nullType;struct emptyType{};template<class H, class T>struct typeList{ typedef H Head; typedef T Tail;};template<class H>struct typeList<H, nullType>{ typedef H Head;};typedef typeList<char, typeList<unsigned char, typeList<wchar_t, nullType> > > charTypes;#define TYPELIST_1(T1) typeList<T1, nullType>#define TYPELIST_2(T1,T2) typeList<T1, TYPELIST_1(T2)>#define TYPELIST_3(T1,T2,T3) typeList<T1, TYPELIST_2(T2,T3)>#define TYPELIST_4(T1,T2,T3,T4) typeList<T1, TYPELIST_3(T2,T3,T4)>template<class H, class T>struct typeAt<typeList<H,T>,0>{ //这行编译错误 typedef H at;};template<class H,class T,size_t idx>struct typeAt<typeList<H,T>,idx>{ typedef typename typeAt<T,idx-1>::at at;};int main(int argc,char**){ typeAt< TYPELIST_4<int,short,char,long>, 2> a=0; return 0;}#include <cstddef>struct nullType;struct emptyType{};template<class H, class T>struct typeList{ typedef H Head; typedef T Tail;};template<class H>struct typeList<H, nullType>{ typedef H Head;};typedef typeList<char, typeList<unsigned char, typeList<wchar_t, nullType> > > charTypes;#define TYPELIST_1(T1) typeList<T1, nullType>#define TYPELIST_2(T1,T2) typeList<T1, TYPELIST_1(T2)>#define TYPELIST_3(T1,T2,T3) typeList<T1, TYPELIST_2(T2,T3)>#define TYPELIST_4(T1,T2,T3,T4) typeList<T1, TYPELIST_3(T2,T3,T4)>template <typename,size_t> struct typeAt;template<class H, class T>struct typeAt<typeList<H,T>,0>{ typedef H at;};template<class H,class T,size_t idx>struct typeAt<typeList<H,T>,idx>{ typedef typename typeAt<T,idx-1>::at at;};int main(int argc,char**){ typename typeAt< TYPELIST_4(int,short,char,long), 2>::at a=0; return 0;}