Ayrosa
Ayrosa

Reputation: 3513

Is there any reason for §3.3.7/1.2 to be considered an error?

This code was obtained from the example in §3.3.7/1.5:

enum { i = 1 };
class X {
    char v[i]; // error: i refers to ::i
    // but when reevaluated is X::i
    enum { i = 2 };
};

GCC emits an error because of §3.3.7/1.2

However, if we apply §3.4.1/7, the lookup for the name i in the declaration char v[i]; will find enum{ i = 1 }; in global scope. What's the problem with the redeclaration enum{ i = 2 };?

Upvotes: 1

Views: 69

Answers (1)

Mark B
Mark B

Reputation: 96311

The problem is that in member function scope (and other class scopes) the class version of the enum shadows the global value.

If it wasn't an error, the compiler would have to pick one of two surprising behaviors:

1) Use the class enum's value always, which means it would have to fully parse the class to determine if a valid size is available.

2) Have the value of i within class members be completely different from the actual length of the class member array declared, notionally, to be the same length.

Upvotes: 3

Related Questions