template<typename... Mixins>
struct myclass : public Mixins... {
myclass(const Mixins&... mixins)
: Mixins(mixins)... { }
};
template<typename... Values>
void foo(const Values&... values) {
boost::any array[sizeof...(Values)] = { values... };
// ...
}
Variadic templates allow a C++ template to accept an arbitrary
number of "extra" template arguments. When used with class
templates, we can write templates
like tuple
(a generalized pair), or arbitrary-level compile-time vectors for
template
metaprogramming. Variadic templates also work in conjunction
with function templates, so that function templates can take an
arbitrary number of function arguments. In this sense, variadic
templates provide the same capabilities
as C's
variadic functions, but in a type-safe manner. For
instance, one can implement a simple, type-safe printf
with the following code:
void printf(const char* s) {
while (*s) {
if (*s == '%' && *++s != '%')
throw std::runtime_error("invalid format string: missing arguments");
std::cout << *s++;
}
}
template<typename T, typename... Args>
void printf(const char* s, const T& value, const Args&... args) {
while (*s) {
if (*s == '%' && *++s != '%') {
std::cout << value;
return printf(++s, args...);
}
std::cout << *s++;
}
throw std::runtime_error("extra arguments provided to printf");
}
Variadic templates solve some serious limitations in current implementations of certain facilities of the C++ Library Extensions Technical Report (TR1). The function object and tuple facilities, for instance, are only barely implementable in current C++. The implementations are very repetitive, requiring the same code to be copied multiple times (each copy supports a different number of arguments). The need to compile all of these copies can actually be quite taxing. The following chart shows compilation times for the GNU TR1 implementation as we increase the number of supported parameters from 1 to 40.

With variadic templates, compilation takes less than a second, and supports any number of arguments. The code is 50k smaller, much clearer, and does not need to make use of any hideous preprocessor metaprogramming tricks.
We hope that variadic templates will be included in the upcoming revision of the ISO C++ standard, dubbed C++0x. The following documents describe variadic templates in more detail.
printf() example above
works.I've modified the GNU C++ Compiler to provide support for variadic templates. To try out variadic templates, just download the GCC 4.1.1 source code (you only need the -core and -g++ parts), apply the patch below, then build and install the compiler as normal. Or, download one of our pre-built binaries. Have fun!
variadicg++ and
is stored in /opt/variadicgcc-4.1.1/bin. It is
identical to GCC 4.1.1 except for the addition of support
variadic templates and the predefined
macro __VARIADIC_TEMPLATES.
We welcome your feedback! Give the compiler a spin, or just read through the proposals and tell us what you think! Douglas Gregor, the primary author, can be reached at .