Questions tagged [template-templates]

A mechanism for allowing templated C++ code to be parameterized not only over types and non-type values, but also over templates of types.

A C++ template can have parameters which are types (such as int), non-types (such as the integer 2) or templates. The latter are called template template parameters.

For example this class template has a template template parameter called Templ and a template type parameter called Type:

template<template<typename> class Templ, typename Type>
  struct Apply
  {
    typedef Templ<Type> type;
  };

To instantiate the class template Apply the argument substituted for Templ must be the name of a class template or alias template taking a single type parameter e.g.

Apply<std::shared_ptr, int>::type ptr;

The nested typedef type is a synonym for std::shared_ptr<int>.

In the ISO C++ Standard, template template parameters are type parameters for convenience (and the name for the grammar production used to define them is called "type-parameter"), so care should be taken when working in the context of the Standard. Conversely, the phrase "non-type template parameter" is a template parameter that is neither a type nor a template in the ISO C++ Standard.

Sometimes, template template parameters are called template templates, but it is better to avoid this term to avoid misunderstandings - the term "template templates" could equally well refer to member templates that are members of class templates, in the absence of any definition in the ISO C++ Standard.

Use this tag for questions about writing and using templates that have template template parameters.

257 questions
315
votes
10 answers

What are some uses of template template parameters?

I've seen some examples of C++ using template template parameters (that is templates which take templates as parameters) to do policy-based class design. What other uses does this technique have?
Ferruccio
  • 98,941
  • 38
  • 226
  • 299
60
votes
3 answers

Variadic template templates and perfect forwarding

This question on the object generator pattern got me thinking about ways to automate it. Essentially, I want to automate the creation of functions like std::make_pair, std::bind1st and std::mem_fun so that instead of having to write a different…
Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
45
votes
3 answers

C++ variadic template template argument that matches any kind of parameters

I was wondering if it's possible to write a template function that can take any other arbitrary template as a parameter and properly match the template name (i.e. not just the resulting class). What I know to work is this: template
Janick Bernet
  • 20,544
  • 2
  • 29
  • 55
34
votes
4 answers

Why is allocator::rebind necessary when we have template template parameters?

Every allocator class must have an interface similar to the following: template class allocator { ... template struct rebind { typedef allocator other; }; }; And classes that use allocators do something…
user541686
  • 205,094
  • 128
  • 528
  • 886
29
votes
2 answers

Befriending a template template parameter

I've got a class template with a template template parameter, and I want to declare this parameter (that is, all of its specializations) as a friend. But I can't find the correct syntax. template