Variadic function templates
Variadic function templates are template functions with a variable number of arguments. They borrow the use of the ellipsis (...
) for specifying a pack of arguments, which can have different syntax depending on its nature.
To understand the fundamentals for variadic function templates, let's start with an example that rewrites the previous min
function:
template <typename T> T min(T a, T b) { return a < b ? a : b; } template <typename T, typename... Args> T min(T a, Args... args) { return min(a, min(args...)); } int main() { std::cout << "min(42.0, 7.5)=" << min(42.0, 7.5) << '\n'; std::cout << "min(1,5,3,-4,9)=" << min(1, 5, 3, -4, 9) <<...