初始模闆元程式設計,感覺真是太cool了,抄一段自《Effective C++》的代碼:
#include <iostream>
template<unsigned n>
struct Factorial {
enum{value = n * Factorial<n - 1>::value};
};
template<>
struct Factorial<0>
{
enum {value = 1};
};
int main()
{
std::cout << "Hello World!\n";
std::cout << Factorial<5>::value << std::endl;
std::cout << Factorial<10>::value << std::endl;
std::cout << Factorial<12>::value;
}
小菜鳥一枚,歡迎指教。