Reputation:
The Standard N4296::3.3.1/4 [basic.scope.declarative]
:
exactly one declaration shall declare a class name or enumeration name that is not a typedef name and the other declarations shall all refer to the same variable or enumerator, or all refer to functions and function templates; in this case the class name or enumeration name is hidden (3.3.10).
I understand that the rule is talking about hiding the name of the class if there is a variable/function declaration with the same name in the same declrartive region. But the the exactly one is a bit confusing. The following namespace is perfectly valid:
namespace A
{
struct A;
struct A { };
int A;
}
although we declared the struct A
twice (two declarations of a struct and one of a variable). What's wrong? What did I lose in the rule?
Upvotes: 1
Views: 234
Reputation: 137770
// Exactly one class may have the name:
struct A; // Declaration of a new class.
struct A { }; // Definition, but not a declaration of a new name. Doesn't count.
// Aside from the class, exactly one variable may share the name:
extern int A; // Declaration of a variable.
int A; // Definition of a variable.
Upvotes: 1
Reputation:
struct A;
is a declaration. However, struct A { };
is a definition.
Upvotes: 0