简体   繁体   中英

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?

It's nothing special really. C++ allows you to refer to a class via an elaborated type specifier . Eg

void foo(class bar*);

This declares a function foo that accepts an argument of the type bar* . If bar was not declared previously, this elaborate type specifier constitutes a declaration of bar in the namespace containing foo . 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 . Said type parameter has a default argument, named by the elaborated class specifier class Z . That declaration can be rewritten just like the function above:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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