Understanding the need for variadic templates
One of the most famous C and C++ functions is printf
, which writes formatted output to the stdout
standard output stream. There is actually a family of functions in the I/O library for writing formatted output, which also includes fprintf
(which writes to a file stream), sprint
, and snprintf
(which write to a character buffer). These functions are similar because they take a string defining the output format and a variable number of arguments. The language, however, provides us with the means to write our own functions with variable numbers of arguments. Here is an example of a function that takes one or more arguments and returns the minimum value:
#include<stdarg.h> int min(int count, ...) { va_list args; va_start(args, count); int val = va_arg(args, int); for (int i = 1; i < count; i++) { int n = va_arg...