靜態元編程(又名「模板元編程」)是一種很棒的C++技術,它允許在編譯時執行程序。一個燈泡,當我讀到這個規範的元編程例子在我的頭上,一旦去了:C++模板元編程的最佳介紹?
#include <iostream>
using namespace std;
template< int n >
struct factorial { enum { ret = factorial< n - 1 >::ret * n }; };
template<>
struct factorial<0> { enum { ret = 1 }; };
int main() {
cout << "7! = " << factorial<7>::ret << endl; // 5040
return 0;
}
如果想了解更多關於C++靜態元編程,什麼是最好的來源(書籍,網站,在線課件, 隨你)?
燈泡熄滅或熄滅? – horseyguy 2010-05-04 18:58:55
關閉。絕對關閉。 – 2012-08-30 19:05:51