David Bakin’s programming blog.


‘typename’ is not always a substitute for ‘class’ in a template parameter

You know the C++ rule that says that the keyword typename can be used interchangably with class in a template parameter?  Not always!  Not for the keyword class that appears after the template parameter of a template template parameter!

This is a syntax error:

template <template <typename> typename T> struct Foo { ... };

You must write it like this:

template <template <typename> class T> struct Foo { ... };

Why is that?  Jonathan Caves explains here that the rule typename can be used interchangably with class is a semantic rule, but the use of class in the template template parameter is a syntax rule (it actually appears as part of the grammer in the standard), and thus takes precedence.

Oversight? Intentional?

By the way, Visual C++ 2012 gives a really unhelpful pair of error messages for this, one of which includes an internal grammar rule name—and that is really non-optimal for a compiler error message:

error C2988: unrecognizable template declaration/definition
error C2059: syntax error: '<L_TEMPLATEDECL>'