C++ 模板模板参数的语法 [英] Syntax of C++ Template Template Parameters

查看:34
本文介绍了C++ 模板模板参数的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解 C++ 模板模板参数的语法.我理解它们为什么有用,根据这里的出色描述,我发现它们的语法很难去了解.取自上述网站的两个示例(还有其他网站):

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;

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.

我意识到我对一个问题的尝试就像一个观察.我所要求的是关于如何在日常口语中解释模板模板参数语法的帮助.我可以用 C++ 语法和我学到的所有其他编程语言来做到这一点.但是,我很难向自己解释"C++ 模板模板参数的语法.我收到了 David Vandevoorde 和 Nicolai M. Josuttis 合着的一本书C++ 模板:完整指南",虽然这是一本不错的书,但它对我理解这种语法并没有太大帮助,我敢肯定许多人会同意充其量是古怪的.

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.

推荐答案

它没有什么神秘之处.只需从原始模板中取出您的模板模板参数:

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_;
};

你会使用你的原始模板

Stack<int, A> s;

这篇关于C++ 模板模板参数的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆