Using recursive templates, I would like to calculate the sum of the first n
integers going from 0 to n
. I.e. SumUp<10>::n
would be 55. I wonder because I'm used to recursion going downwards (ie. from n
to 0). Is it possible to go from 0 to n? Here's my attempt:
template<int n> struct SumUp {
static constexpr int n = n;
};
template<> struct SumUp<0> {
static constexpr int n = 0 + SumUp<n + 1>::n; // <- Problem
};
The problem is, that in the second template, n
is not known and therefore doesn't compile.