Discussion
Functions taking a variable number of arguments are a nice commodity, but C-style varargs aren't the way to get them. Varargs have many serious shortcomings:
Lack of type safety:
Essentially, the ellipsis tells the compiler: "Turn all checking off. I'll take over from here and start reinterpret_casting." (See Item 92.)
Tight coupling and required manual cooperation between caller and callee:
The language's type checking has been disabled, so the call site must use alternate ways to communicate the types of the arguments being passed. Such protocols (e.g., printf's format string) are notoriously error-prone and unsafe because they cannot be fully checked or enforced by either the caller or the callee. (See Item 99.)
Undefined behavior for objects of class type:
Passing anything but primitive and POD (plain old data) types via varargs has undefined behavior in C++. Unfortunately, most compilers don't give a warning when you do it.
Unknown number of arguments:
For even the simplest variable-arguments function (e.g., min) with a variable number of arguments of known type (e.g., int), you still need to have a protocol for figuring out the number of arguments. (Ironically, this is a good thing because it further discourages using varargs.)
Avoid using varargs in your functions' signatures. Avoid calling functions with varargs in their own signatures, including legacy functions and standard C library functions such as sprintf. Admittedly, calls to sprintf can often look more compact and easier to read than equivalent calls using stringstream formatting and operator<<just like it's also admittedly easier to hop into a car without pesky safety belts and bumpers. The risks are just not worth it. printf-related vulnerabilities continue to be a serious security problem at the time of this writing (see [Cowan01]), and an entire subindustry of tools exists to help find such type errors (see [Tsai01]).
Prefer to use type-safe libraries that support variable arguments using other means. For example, the [Boost] format library uses advanced C++ features to combine safety with speed and convenience.
 |