开发者

Syntax of C++ Template Template Parameters

开发者 https://www.devze.com 2023-04-02 13:53 出处:网络
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 un

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 a开发者_StackOverflow社区nd 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.


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


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消