Is `template<typename>;` legal C++?

  • A+

GCC and Clang disagree on whether template<typename>; is a valid statement in C++ at global scope.

I'd expect it not to be allowed in the C++ standard because templatization pertains to declaration statements, not to expression statements and in consequence also not to null statements (the statement ;).

So, is this a bug in Clang?

Not really. The standard explicitly disallows such a declaration in [temp]p2;

The declaration in a template-declaration (if any) shall

  • declare or define a function, a class, or a variable, or

  • define a member function, a member class, a member enumeration, or a static data member of a class template or of a class nested within a class template, or

  • define a member template of a class or class template, or

  • be a deduction-guide, or

  • be an alias-declaration.

An empty-declaration doesn't match any of those clauses. Now the standard says that an implementation is required to issue a diagnostic message for any violation of its rules, like this one. Note that it says diagnostic, it doesn't specify whether a warning or an error (or even a note) is issued. The compiler can provide extensions that make what you wrote valid, as it wouldn't change the meaning of a well-formed program.

So no, both are right. However, clang's behavior is due to an extension, not something that the standard specifies.


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