C++ 的语法模板 模板参数

发布于 2024-12-02 16:01:31 字数 658 浏览 0 评论 0原文

我很难理解 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三生池水覆流年 2024-12-09 16:01:32

我不确定你的问题到底是什么,但这是对你给出的两个例子的解释。

template <typename T, template <typename> class Cont>
class Stack;

Stack 是一个具有两个模板参数的类模板。第一个参数T可以是任何类型(包括内置类型、用户定义类型、模板实例化等)。第二个参数 Cont 必须是带有一个参数的类模板。该参数未命名,因为它没有多大意义(该参数永远不会绑定到任何东西)。

template <template <typename,typename> class Cont>
class Wrapper3;

Wrapper3 是一个具有单个参数Cont 的类模板。 Cont 必须是具有两个参数的类模板。

定义模板模板参数的语法与定义类模板的语法相同(templateclass Name),因此我真的不明白你的问题是什么。

但是,我同意,当您开始“嵌套”模板模板参数时,语法可能会变得有点尴尬:

// 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 {};

不过,这种情况并不经常发生......

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

心凉 2024-12-09 16:01:32

没有什么那么神秘的。只需从原始模板中取出模板模板参数:

template <typename> class Cont

任何具有单个类型参数的类模板都适合,例如

template <typename T>
class A {
public:
  A(T t) : t_(t) {}
  T get() { return t_; }
private:
  T t_;
};

您将使用原始模板作为

Stack<int, A> s;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文