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
-2
votes
1 answer

C++20 Pass Familiar Template lambda as Class Template to Template Parameter of a Class

Consider passing a callable class template as a template parameter to some class. Three approaches as below but only functor one works. The naive template function failed because it cannot serve as a class template; However, why the C++20 familiar…
Nkk
  • 53
  • 1
  • 5
-2
votes
2 answers

Implementing partial template specialization of template template parameter

I am having problems implementing the specialization of a class template that uses template template parameters. For example, I would like to write a class that is used for sorting: template