[英]Syntax of C++ Template Template Parameters
I'm having difficulty understanding the syntax of C++ Template Template parameters. 我很难理解C ++模板模板参数的语法。 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.
我可以用C ++语法和我学到的所有其他编程语言来做到这一点。 However I'm having difficulty "explaining" the syntax of C++ Template Template parameters to myself.
但是我很难向自己“解释”C ++模板模板参数的语法。 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.
我收到了一本书,“C ++模板:David Vandevoorde和Nicolai M. Josuttis的完整指南”,虽然它是一本很好的书,但对我理解这种语法并没有多大帮助,我确信很多人会同意最好是古怪的。
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. Stack
是一个包含两个模板参数的类模板。 The first parameter, T
can be any type (including built-in types, user-defined types, template instantiations and so on). 第一个参数
T
可以是任何类型(包括内置类型,用户定义类型,模板实例化等)。 The second parameter, Cont
, must be a class template taking one parameter. 第二个参数
Cont
必须是带有一个参数的类模板。 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
. Wrapper3
是一个带有单个参数Cont
的类模板。 Cont
must be a class template with two parameters. Cont
必须是具有两个参数的类模板。
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. 定义模板模板参数的语法与定义类模板(
template <typename [param1], typename [param2], ...> class Name
)的语法相同,所以我真的不明白你的是什么问题。
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.