John Kalane
John Kalane

Reputation: 1185

Doubts about [dcl.spec]/3 in c++1z

[dcl.spec]/3 in c++1z (emphasis is mine):

If a type-name is encountered while parsing a decl-specifier-seq, it is interpreted as part of the decl-specifier-seq if and only if there is no previous defining-type-specifier other than a cv-qualifier in the decl-specifier-seq. The sequence shall be self-consistent as described below. [ Example:

typedef char* Pc;
static Pc;                      // error: name missing

Here, the declaration static Pc is ill-formed because no name was specified for the static variable of type Pc. To get a variable called Pc, a type-specifier (other than const or volatile) has to be present to indicate that the typedef-name Pc is the name being (re)declared, rather than being part of the decl-specifier sequence.

Nevertheless, the snippet below doesn't compile in g++ and clang. Why is that?

typedef char* Pc;
static int Pc;

Upvotes: 2

Views: 97

Answers (1)

The intent of the paragraph is to show that typedef char* Pc is valid, whereas static Pc is not, not that you can redeclare Pc as something else (in the same scope). In other words, static Pc and static int Pc are not different entities. Nothing's changed that would allow you to do this.

[basic.scope.declarative]/4

Given a set of declarations in a single declarative region, each of which specifies the same unqualified name,

(4.1) — they shall all refer to the same entity, ...

Upvotes: 3

Related Questions