user2953119
user2953119

Reputation:

Declarations of a class name and a variable with the same name

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;
}

DEMO

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

Answers (2)

Potatoswatter
Potatoswatter

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

user319799
user319799

Reputation:

struct A; is a declaration. However, struct A { }; is a definition.

Upvotes: 0

Related Questions