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
12
votes
4 answers

template template function parameter

For the life of me, I can't get this simple piece of arcane template magic to work: template int f(T v){ return v*a-b; // just do something for example } template
Emily L.
  • 5,673
  • 2
  • 40
  • 60
11
votes
1 answer

Template template parameter and default values

Consider the following code: template struct A { }; // same as A, but with one extra defaulted parameter template struct B { }; template typename T> T build() { return {}; } int…
Floop
  • 451
  • 4
  • 10
10
votes
5 answers

Pass a template template argument without specifying the concrete type

I would like to implement a wrapper around a function call that would do something like this: template class F> void Wrapper(int n, F&& f) { switch (n) { case 1: f(); break; …
undermind
  • 1,779
  • 13
  • 33
10
votes
2 answers

How to write a c++ template that works for both a map and vector of pair>

I'd like to write a template function that iterates over a container of std::pair and returns a templated value with both types in the pair. I've gotten this to work for std::map as follows: template
jonthalpy
  • 1,042
  • 8
  • 22
10
votes
1 answer

Template alias, Template specialization and Template Template parameters

I want to determine the underlying template of a template parameter by using a combination of a template alias and template specializations. The follwing code compiles fine on gcc 4.8, 6.2.1 but not on clang 3.5, 3.8. #include template…
10
votes
2 answers

Variadic variadic template template parameters

Is there a straightforward way to have variadic variadic template template parameters. For instance consider the following function signature template class Pack, typename T, size_t ... Args> void foo(const…
10
votes
4 answers

Altering template template parameters in C++

I would like to design a class that creates internal types that are variants of types passed as template parameters. Something like the following, non-functional example: template class BaseClass { public: typedef T InternalType; …
10
votes
1 answer

Template template class with enum specification fails on MSVC++ Compiler: C3201

Code Here is the SSCCE example of my problem: // My Library, which I want to take in the user's enum and a template class which they put per-enum specialized code template class EnumStruct> struct LibraryT { /*…
Bob Fincheimer
  • 17,978
  • 1
  • 29
  • 54
9
votes
1 answer

Is there such a thing as a function template template parameter?

So I know C++ has a feature called "template template parameters", where you can pass a class template as a template parameter. For example: template class vector { ... }; template