In C and C++, a function can have optional function arguments. For example,
int printf (const char *template, ... )
How about we wrapp this function? For example,
int printError(const char* template, ...) { // additional logic // an invocation of printf // additional logic }
In fact, there is no way to passing the optional arguments of printError to printf. The solution is to use va_list. For this example, we can can use vprintf.
int printError(const char* template, ...) { // additional logic va_list ap; va_start(ap, template); vprintf(template, ap); va_end(ap); // additional logic }
If we want to define some API with optional arguments, we use optional arguments in the exposed API. But for the internal implementation, we should use va_list since va_list can be passed from function definition to function defintion.
For details, you can refer to 12.12.9 Variable Arguments Output Functions in The GNU C Library Reference Manual .