I'm trying to write a meta function with recursion. The inputs are variadic integers, and the output should be the sum of the inputs.
My code like follows:
template <size_t curInput, size_t...Inputs>
constexpr size_t Accumulate = curInput + Accumulate<Inputs...>;
// template specialization
template <size_t...Inputs>
constexpr size_t Accumulate<Inputs> = 0;
int main(int argc, char *argv[]) {
constexpr size_t res1 = Accumulate<1>;
constexpr size_t res2 = Accumulate<1, 2, 3, 4, 5>;
return 0;
}
With the test in main(), the res1 is 0, and res2 is 10. It seems like treat the last integer as 0, I don't understand why this happened. And I want to know how to modify it.
Any reply will be appreciated!
template <std::size_t...Inputs> constexpr std::size_t Accumulate<Inputs> = (0 + ... + Inputs);.