Variadic Templates are Fun—and Now Legal

Abstract

Variadic templates are arguably the most profound change in the core language brought about by C++0X. Curiously, however, the C++ community still tiptoes carefully around them: variadic templates enjoyed less coverage than features such as “auto” or lambdas. Part of the reason is that more popular features simplify expression of existing designs, which makes said features easier to understand and use. Variadic templates, however, not only simplify design and use of advanced libraries such as Boost MPL, but also enable a host of new uses.

This class provides a solid coverage of variadic fundamentals, including typelists, the archetypal “safe printf” mechanics, and tuple construction and access. It also discusses more advanced uses, such as structured argument lists.

A function with a structured argument list accepts variadic arguments that have a structure to them, most often expressible as a regular expression. For example, a function may accept argument lists of the form (string, string, int, string, int, string, …), i.e. two strings followed by any number of int and string pairs. Structured argument lists are very useful in supporting implementation of fast, expressive algorithms and for defining a variety of useful abstractions.

Highlights

  • Describes fundamentals of variadic templates, both from a type system/theoretical and practical/applicability perspective
  • Introduces true, typesafe variadic functions
  • Redefines typelists in terms of variadic templates
  • Introduces a complete implementation of std::tuple 

Attendee Profile

This is a C++ intensive class. Participants should be comfortable with advanced uses of templates, such as typelists and partial specialization.

Outline

  • Motivation and fundamentals
    • A new kind: parameter packs
    • Using parameter packs
    • Expansion rules with quick mnemonic
    • Expansion loci
    • Using variadics
  • True variadic functions
    • Typesafe printf
    • Step 1: add adaptation routines
    • Step 2: define test for arg-less call
    • Step 3: define recursive test
    • Step 4: integration
    • Further improvements
  • Typelists
    • A lesson from Lisp
    • First-class parameter packs
    • Simpler head, tail
    • cons
    • Append, reverse
    • Typelist aftermath
  • std::tuple
    • Introduction
    • The usual suspects
    • Less usual suspects
    • Structure
    • Construction
    • Conversion from lvalue and rvalue
    • Implementing std::get
  • Conclusion