静态元编程(也称为“模板元编程”)是一种优秀的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++静态元编程的知识,最好的资源(书籍、网站、在线课程等)是什么?