C++ 的语法模板 模板参数
我很难理解 C++ 模板模板参数的语法。我理解它们为什么有用,根据此处的精彩描述,我只是发现它们的语法很难去理解。取自上述网站的两个示例(还有其他网站):
template <typename T, template <typename> class Cont>
class Stack;
如果
template <template <typename,typename> class Cont>
class Wrapper3;
不了解此语法背后的基本原理,就不可能清楚地概括此类声明。记忆比较困难,而且似乎没有多大帮助。
编辑: 我意识到我对一个问题的尝试就像一次观察。我要求的是有关如何解释日常用语中的模板参数语法的帮助。我可以使用 C++ 语法和我学到的所有其他编程语言来完成此任务。然而,我很难向自己“解释”C++ 模板模板参数的语法。我得到了一本书,《C++ 模板:完整指南》,作者是 David Vandevoorde 和 Nicolai M. Josuttis,虽然这是一本好书,但我确信它对我理解这种语法没有太大帮助许多人都会同意这充其量是古怪的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定你的问题到底是什么,但这是对你给出的两个例子的解释。
Stack
是一个具有两个模板参数的类模板。第一个参数T
可以是任何类型(包括内置类型、用户定义类型、模板实例化等)。第二个参数Cont
必须是带有一个参数的类模板。该参数未命名,因为它没有多大意义(该参数永远不会绑定到任何东西)。Wrapper3
是一个具有单个参数Cont
的类模板。Cont
必须是具有两个参数的类模板。定义模板模板参数的语法与定义类模板的语法相同(
templateclass Name
),因此我真的不明白你的问题是什么。但是,我同意,当您开始“嵌套”模板模板参数时,语法可能会变得有点尴尬:
不过,这种情况并不经常发生......
I am not sure what is your question exactly, but here is the explanation for the two examples you gave.
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).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:
Doesn't happen that often, though...
没有什么那么神秘的。只需从原始模板中取出模板模板参数:
任何具有单个类型参数的类模板都适合,例如
您将使用原始模板作为
There's nothing so arcane about it. Just take out your template template parameters from the original template:
Any class template with a single type argument fits, such as
And you would use your original template as