今天第一次听说模板元编程,或者之前没在意,见到了还以为是和模板编程一样的概念,结果今天在看何海涛的面试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;
}

相关文章:

  • 2021-06-16
  • 2021-12-12
  • 2022-01-10
  • 2022-12-23
  • 2022-12-23
  • 2021-10-29
  • 2022-02-23
猜你喜欢
  • 2022-01-02
  • 2022-12-23
  • 2022-02-14
  • 2022-12-23
  • 2021-10-31
  • 2021-08-24
相关资源
相似解决方案