今天第一次听说模板元编程,或者之前没在意,见到了还以为是和模板编程一样的概念,结果今天在看何海涛的面试100题第9题的时候发现下面有个解法颇为不解,查了一下,叫模板元编程。她给出的解法是这样的。
#include<iostream>using namespace std;template<int N>class SUM{public:const static int value = N+SUM<N-1>::value;};template<>class SUM<1>{public:const static int value = 1;};int main(){cout<<SUM<10>::value<<endl;return 0;}