6

I'm having difficulty understanding the syntax of C++ Template Template parameters. I understand why they are useful, as per the excellent description here, I just find their syntax hard to get to understand. Two examples taken from the above website (there are others):

template <typename T, template <typename> class Cont>
class Stack;

and

template <template <typename,typename> class Cont>
class Wrapper3;

Clearly generalizing such declarations is impossible without some understanding of the rationale behind this syntax. Memorizing is harder and does not seem to be of much help.

Edit: I realize that my attempt at a question came across like an observation. What I'm asking for is help on how to interprete the Template Template parameter syntax in everyday speak. I can do this with the C++ syntax and the all the other programming languages that I've learned. However I'm having difficulty "explaining" the syntax of C++ Template Template parameters to myself. I've gotten a book, "C++ templates : the complete guide" by David Vandevoorde and Nicolai M. Josuttis, and while its a nice book, it hasn't been of much help to me in understanding this syntax which I'm sure many will agree is at best quirky.

Olumide
  • 5,397
  • 10
  • 55
  • 104

2 Answers2

18

I am not sure what is your question exactly, but here is the explanation for the two examples you gave.

template <typename T, template <typename> class Cont>
class Stack;

Stack is a class template with two template parameters. The first parameter, T can be any type (including built-in types, user-defined types, template instantiations and so on). The second parameter, Cont, must be a class template taking one parameter. The parameter is unnamed because it would not make much sense (the parameter is never bound to anything).

template <template <typename,typename> class Cont>
class Wrapper3;

Wrapper3 is a class template with a single parameter, Cont. Cont must be a class template with two parameters.

The syntax to define a template template parameter is the same as the one to define a class template (template <typename [param1], typename [param2], ...> class Name), so I don't really understand what is your problem.

However, I agree that the syntax can become a bit awkward when you start "nesting" template template parameters:

// class template whose parameter must be a class template whose parameter
// must be a class template
template <template <template <typename> class > class C >
struct Wow {};

Doesn't happen that often, though...

Luc Touraille
  • 79,925
  • 15
  • 92
  • 137
  • _"The [template template] parameter is unnamed because it would not make much sense (the parameter is never bound to anything)"_. Does this mean `template – mucaho Nov 10 '15 at 14:09
  • 1
    @mucaho You can give a name to the parameter, but you cannot use it, because it is never bound to any type. For instance, assume you have the class `template class Bar {}`: you can use it to instantiate `Foo` like this: `Foo f`. What is the type represented by E in this case? If you want to write `Foo>`, then you don't need a template template parameter, just a regular non-template parameter. – Luc Touraille Nov 10 '15 at 15:22
4

There's nothing so arcane about it. Just take out your template template parameters from the original template:

template <typename> class Cont

Any class template with a single type argument fits, such as

template <typename T>
class A {
public:
  A(T t) : t_(t) {}
  T get() { return t_; }
private:
  T t_;
};

And you would use your original template as

Stack<int, A> s;
Nicola Musatti
  • 17,834
  • 2
  • 46
  • 55