2006-08-20

Douglas Gregor on Variadic Templates

Doug is a post-doctoral research fellow in the Open Systems Lab, part of the Computer Science department at Indiana University, Bloomington. His email is dgregor -at- cs.indiana.edu and his home page is http://www.osl.iu.edu/~dgregor.

//: You've just finished implementing Variadic Templates. What the heck are Variadic Templates?

Gregor: Variadic templates are templates that can take an arbitrary number of "extra" template arguments. Most templates take a fixed number of parameters. For example, an STL map has 4 parameters (Key, Data, Compare, Alloc) while an STL pair has two parameters (T and U). But what happens when you want to generalize a pair into a tuple (for any N), like in the Boost Tuples library? You really need tuple to take any number of template arguments: variadic templates allow you to do so, by creating class and function templates that accept any number of arguments. Variadic templates also allow you to perform transformations on all of these arguments in one step, turning many non-trivial metaprograms into simple one-line statements.

//: C++ programmers are familiar with the concept with variadic function parameters from C. But we've been cautioned to avoid them. Why are Variadic Templates safer?

Gregor: C-style variadic functions are unsafe because all type information is lost when passing arguments through C's "...". That's why only built-in and Plain Old Data types work with with C-style variadic functions, and even with those we're left wide open to format string attacks.

Variadic templates are safer because, as templates, they retain complete type information at compile-time. With printf() implemented as a variadic template, we don't need the format string to tell us the type of each argument, because we have its static type when we instantiate the template. So instead of crashing when printf() is given a format string that doesn't match the actual arguments, we can just detect the error and throw an exception.

//: How are VTs helpful? What do they make possible, easier, or better?

Gregor: Variadic templates make it easier to write templates that work with any number of arguments. You can fake variadic templates in the language, but the result is usually a mess of duplicated code, artificial upper limits on the number of template arguments, and preprocessor meta-programming. If you don't believe me, look at the implementation of Boost's Function, Bind, or Tuple libraries: we can shrink the implementation of these libraries from tens of thousands of lines of redundant code to a few pages of relatively simple code. Perhaps best of all, we can bring the March of Progress full circle, providing the succinct (and popular!) syntax of printf and scanf for all of C++, providing the type safety that we expect from the language. Although we haven't explored it in detail yet, we've also found that variadic templates are very powerful tools for template metaprogramming.

//: Where can we learn more about how to use them?

Gregor: The best source of information on variadic templates is in the introduction and proposal on the variadic templates web page: http://www.generic-programming.org/~dgregor/cpp/variadic-templates.html.

The introduction gives a short description of the implementation of a completely type-safe printf using variadic templates. The (much longer) proposal provides annotated implementations of several interesting facilities that make heavy use of variadic templates. Like all language features, the best way to understand a feature is to try it out: the examples in the proposal are a good starting point.

//: This sounds like a language extension that can't be implemented with a library. Is this a compiler mod? Which compiler? How can we get it to play with?

Gregor: Variadic templates cannot be implemented purely in a library, so I've implemented them in the GNU C++ compiler. Patches against the latest version of GCC are available from http://www.generic-programming.org/~dgregor/cpp/variadic-templates.html. I encourage anyone interested in variadic templates to give the compiler a try: even after thinking about variadic templates for four years, it wasn't until I got to play with them that I really understood how they worked. Of course, I welcome any feedback!

//: So when will this be part of the official GCC release?

Gregor: I expect to officially submit the variadic templates patches to GCC in the very near future. After that, the decision of when to ship variadic templates is in the hands of the GCC maintainers. The good news is that variadic templates have been discussed before on the GCC mailing lists, and there is an open feature request from one of the maintainers for its inclusion. And unlike our other GCC hack, the implementation of variadic templates is quite small and quite stable. Optimistically, I think we could see variadic template support in the official GCC 4.2.1 or 4.3.0.

//: You are on the C++ Standards committee. Does this mean that we'll see VTs become part of the C++ Standard?

Gregor: I hope so. I will bring the latest variadic templates proposal to the upcoming C++ committee meeting in Portland, but the C++ committee as a whole decides what will go into the next C++ standard. Having a complete implementation in GCC helps greatly (implementability is the first major hurdle for a proposal), but the biggest factor for acceptance is whether the community wants this feature in the language. In particular, variadic templates have always been viewed as a niche feature for expert library developers. Do you like variadic templates? Write your local C++ committee representative or show your support on comp.std.c++!

//: Who do you think authored the Shakespearean canon?

Gregor: Alas, I think the truth of the matter is that William Shakespeare was "merely" a brilliant man, and any authorship questions are unnecessary drama. In truth, I am more interested in the mathematical side of things, and the story of Nicolar Bourbaki.

0 comments:

Post a Comment

links to this post:

Create a Link

<< home