Syntax
1 2 |
template <typename T, typename... Args> void printf(const char *format, T value, Args... args); |
1 2 3 4 5 6 7 |
template <typename T> long long getSumOfPows(T t) { return t*t; } template <typename T, typename... Rest> long long getSumOfPows(T t, Rest... rest) { return getSumOfPows(t) + getSumOfPows(rest...); } long mySum = getSumOfPows(1,2,3,4,5,6,7); |
- Variadic templates essentially allow recursion to be replaced by iteration
- Variadic templates needs an end function – usually it’s a specialization for the last remaining parameter, in order to stop the recursion. This has changed in C++17 and is no longer needed.