Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 844
Duns Avatar asked Jan 27 '19 13:01

Duns


1 Answers

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

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. I.e. 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
  {};
like image 190
StoryTeller - Unslander Monica Avatar answered Oct 04 '22 21:10

StoryTeller - Unslander Monica