// Prime number computation by Erwin Unruh template <int i> struct D { D(void*); operator int(); };
template <int p, int i> struct is_prime { enum { prim = (p%i) && is_prime<(i > 2 ? p : 0), i -1> :: prim }; };
template < int i > struct Prime_print { Prime_print<i-1> a; enum { prim = is_prime<i, i-1>::prim }; void f() { D<i> d = prim; } };
struct is_prime<0,0> { enum {prim=1}; }; struct is_prime<0,1> { enum {prim=1}; }; struct Prime_print<2> { enum {prim = 1}; void f() { D<2> d = prim; } }; #ifndef LAST #define LAST 10 #endif main () { Prime_print<LAST> a; }
类模板D只有一个参数为void*的构造器,而只有0才能被合法转换为void*。1994年,Erwin Unruh采用Metaware 编译器编译出错信息如下(以及其它一些信息,简短起见,它们被删除了): | Type `enum{}′ can′t be converted to txpe `D<2>′ ("primes.cpp",L2/C25). | Type `enum{}′ can′t be converted to txpe `D<3>′ ("primes.cpp",L2/C25). | Type `enum{}′ can′t be converted to txpe `D<5>′ ("primes.cpp",L2/C25). | Type `enum{}′ can′t be converted to txpe `D<7>′ ("primes.cpp",L2/C25). 如今,上面的代码已经不再是合法的C++程序了。以下是Erwin Unruh亲手给出的修订版,可以在今天符合标准的C++编译器上进行编译:
// Prime number computation by Erwin Unruh
template <int i> struct D { D(void*); operator int(); };
template <int p, int i> struct is_prime { enum { prim = (p==2) || (p%i) && is_prime<(i>2?p:0), i-1> :: prim }; };
template <int i> struct Prime_print { Prime_print<i-1> a; enum { prim = is_prime<i, i-1>::prim }; void f() { D<i> d = prim ? 1 : 0; a.f();} };
template<> struct Prime_print<1> { enum {prim=0}; void f() { D<1> d = prim ? 1 : 0; }; };
#ifndef LAST #define LAST 18 #endif
main() { Prime_print<LAST> a; a.f(); } 在GNU C++ (MinGW Special) 3.2中编译这段程序时,编译器将会给出如下出错信息(以及其它一些信息,简短起见,它们被删除了): Unruh.cpp:12: initializing argument 1 of `D<i>::D(void*) [with int i = 17]' Unruh.cpp:12: initializing argument 1 of `D<i>::D(void*) [with int i = 13]' Unruh.cpp:12: initializing argument 1 of `D<i>::D(void*) [with int i = 11]' Unruh.cpp:12: initializing argument 1 of `D<i>::D(void*) [with int i = 7]' Unruh.cpp:12: initializing argument 1 of `D<i>::D(void*) [with int i = 5]' Unruh.cpp:12: initializing argument 1 of `D<i>::D(void*) [with int i = 3]' Unruh.cpp:12: initializing argument 1 of `D<i>::D(void*) [with int i = 2]' 这个例子展示了可以利用模板实例化机制于编译期执行一些计算。这种通过模板实例化而执行的编译期计算技术即被称为模板元编程。
template <typename T> inline T sum_array(int Dim, T* a) { T result = T(); for (int i = 0; i < Dim; ++i) { result += a[i]; } return result; } 这当然可行,但我们也可以利用模板元编程技术来解开循环: // sumarray2.h
// 原始模板 template <int Dim, typename T> class Sumarray { public: static T result(T* a) { return a[0] + Sumarray<Dim-1, T>::result(a+1); } };
// 作为终结准则的局部特化版 template <typename T> class Sumarray<1, T> { public: static T result(T* a) { return a[0]; } };
模板元编程技术并非都是优点,比方说,模板元程序编译耗时,带有模板元程序的程序生成的代码尺寸要比普通程序的大,而且通常这种程序调试起来也比常规程序困难得多。另外,对于一些程序员来说,以类模板的方式描述算法也许有点抽象。 编译耗时的代价换来的是卓越的运行期性能。通常来说,一个有意义的程序的运行次数(或服役时间)总是远远超过编译次数(或编译时间)。为程序的用户带来更好的体验,或者为性能要求严格的数值计算换取更高的性能,值得程序员付出这样的代价。 很难想象模板元编程技术会成为每一个普通程序员的日常工具,相反,就像Blitz++和Loki那样,模板元程序几乎总是应该被封装在一个程序库的内部。对于库的用户来说,它应该是透明的。模板元程序可以(也应该)用作常规模板代码的内核,为关键的算法实现更好的性能,或者为特别的目的实现特别的效果。 模板元编程技术首次正式亮相于Todd Veldhuizen的Using C++ Template Metaprograms论文之中。这篇文章首先发表于1995年5月的C++ Report期刊上,后来Stanley Lippman编辑C++ Gems一书时又收录了它。参考文献中给出了这篇文章的链接,它还描述了许多本文没有描述到的内容。 David Vandevoorde和Nicolai M. Josuttis合著的C++ Templates: The Complete Guide一书花了一整章的篇幅介绍模板元编程技术,它同样是本文的参考资料并且也应该作为你的补充阅读材料。 Andrei Alexandrescu的天才著作Modern C++ Design: Generic Programming and Design Patterns Applied的第3章Typelists对Typelist有着更为详尽的描述。