Is there a smart way to calculate a sum of consecutive numbers via template meta programming? This would be the non template algorithm for e.g 1 to 100.
int i = 0;
for (int n = 1; n <= 100; n++)
i += n;
return i;
I thought about using a variadic add function and fill it with a parameter list. However I am not sure how to create a parameter list.
// add function
template<typename T>
T add(T a) {
return a;
}
template<typename T, typename... Args>
T add(T a, Args... args) {
return a + add(args...); // a + a + a + .. + add()
}