The following code yields an error error: ‘struct Foo’ is not a valid type for a template constant parameter:
template <struct Foo>
struct Bar {
};
Why is that so?
template <class Foo>
struct Bar {
};
works perfectly fine and even accepts an struct as argument.
This is just an artifact of the syntax rules – the syntax just lets you use the
classortypenamekeywords to indicate a type template parameter. Otherwise the parameter has to be a ‘non-type’ template parameter (basically an integral, pointer or reference type).I suppose Stroustrup (and whoever else he might have taken input from) decided that there was no need to include
structas a a keyword to indicate a type template parameter since there was no need for backwards compatibility with C.In fact, my recollection
(I’ll have to do some book readin’ when I get back home)is that whentypenamewas added to indicate a template type parameter, Stroustrup would have liked to take away using theclasskeyword for that purpose (since it was confusing), but there was too much code that relied on it.Edit:
Turns out the story is more like (from a blog entry by Stan Lippman):