Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 844
Fytch Avatar asked May 20 '18 10:05

Fytch


People also ask

Should I use typename or class for template?

When specifying a template template, the class keyword MUST be used as above -- it is not interchangeable with typename in this case (note: since C++17 both keywords are allowed in this case).

What is typename in template?

" typename " is a keyword in the C++ programming language used when writing templates. It is used for specifying that a dependent name in a template definition or declaration is a type.

Are C++ templates type safe?

C++ templates are checked at least twice. First, when a template is declared & defined, second when it is instantiated. After a template successfully instantiated it is in a type safe state.

What's the difference between template and class?

An individual class defines how a group of objects can be constructed, while a class template defines how a group of classes can be generated. Note the distinction between the terms class template and template class: Class template.


2 Answers

It's clang's idiosyncratic behavior that has existed for long time: missing declarations generate only a warning. It's just same as this:

int;

g++ would show an error, while clang will only show a warning. This doesn't contradict the standard.

warning: declaration does not declare anything [-Wmissing-declarations]

-Werror=missing-declarations sets things straight.

like image 66
Swift - Friday Pie Avatar answered Oct 17 '22 07:10

Swift - Friday Pie


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.

like image 37
Rakete1111 Avatar answered Oct 17 '22 05:10

Rakete1111