templatestruct myclass : public Mixins... { myclass(const Mixins&... mixins) : Mixins(mixins)... { } };
templatevoid 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++; } } templatevoid 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.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 .