Default template parameter with class

  • A+
Category:Languages

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

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: