简体   繁体   English

带类的默认模板参数

[英]Default template parameter with class

I've just found out about a strange syntax for default template parameters 我刚刚发现了一个关于默认模板参数的奇怪语法

template<class T = class Z>
struct X
  {};

What does the second "class" keyword mean in this context? 第二个“class”关键字在这种情况下意味着什么?

It's nothing special really. 真的没什么特别的。 C++ allows you to refer to a class via an elaborated type specifier . C ++允许您通过详细的类型说明符引用类。 Eg 例如

void foo(class bar*);

This declares a function foo that accepts an argument of the type bar* . 这声明了一个函数foo ,它接受bar*类型的参数。 If bar was not declared previously, this elaborate type specifier constitutes a declaration of bar in the namespace containing foo . 如果之前没有声明bar ,那么这个精巧的类型说明符构成了包含foo的命名空间中的bar声明。 Ie as if you had written: 就好像你写过:

class bar;
void foo(bar*);

Back to your example, X is a class template that expects a single type parameter, denoted by class T , but could have been denoted just the same as typename T . 回到您的示例, X是一个类模板,需要单个类型参数,由class T表示,但可以表示为与typename T相同。 Said type parameter has a default argument, named by the elaborated class specifier class Z . 所述类型参数具有默认参数,由详细的类说明符class Z命名。 That declaration can be rewritten just like the function above: 该声明可以像上面的函数一样重写:

class Z;
template<class T = Z>
struct X
  {};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM