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
9
votes
2 answers

C++11 template alias as template template argument leads to different type?

We have observed a strange behaviour in the compilation of the follwing source code: template class TT> struct X { }; template struct Y { }; template using Z = Y; int main() { X y; X z; z = y; // it…
craffael
  • 373
  • 1
  • 10
9
votes
1 answer

Are template template template parameters an extension or part of the standard?

I was searching for something else related to template template parameters and happened upon this answer which claims that template template template parameters are not allowed by the standard. However, the following code compiles in the latest…
celticminstrel
  • 1,637
  • 13
  • 21
8
votes
1 answer

Why is the concept in template template argument not verified?

C++20 allows the program to specify concept for template template argument. For example, #include template concept Char = std::same_as; template struct S {}; template