Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax of C++ Template Template Parameters

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.

like image 236
Olumide Avatar asked Sep 02 '11 11:09

Olumide


People also ask

What is the syntax of template?

A class template starts with the keyword template followed by template parameter(s) inside <> which is followed by the class declaration. template <class T> class className { private: T var; ... .. ... public: T functionName(T arg); ... .. ... };

What is template type parameter?

A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.

How do you name a template parameter?

If there is just a single template parameter, I name it T (or U,V for nested templates). When there are multiple parameters and the use is not immediately obvious then I use descriptive names prefixed with T. For example, TKey, TValue, TIdentifiier, etc ...

What is the correct syntax of defining function template template functions?

What is the correct syntax of defining function template/template functions? Explanation: Starts with keyword template and then <class VAR>, then use VAR as type anywhere in the function below. 7.


1 Answers

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...

like image 171
Luc Touraille Avatar answered Oct 22 '22 11:10

Luc Touraille