下面两篇文章都介绍了模板元编程,enum是其最重要的基本工具 http://www.codeproject.com/Articles/3743/A-gentle-introduction-to-Template-Metaprogramming https://www10.informatik.uni-erlangen.de/~pflaum/pflaum/ProSeminar/meta-art.html
因此可以得道以下结论:
下面是简单的例子, 一个求N的阶乘的代码:
#include <iostream>
template<int N>
class Factorial {
public:
enum { RESULT = N * Factorial<N - 1>::RESULT };
};
template<>
class Factorial<1> {
public:
enum { RESULT = 1 };
};
int main(int argc, char ** argv) {
try {
std::cout << Factorial<4>::RESULT << std::endl;
} catch(std::exception const& e) {
std::cerr << e.what() << std::endl;
}
}原文:http://blog.csdn.net/csfreebird/article/details/44888757