What rule makes the following code compile without error:
using integer = int;
struct Foo
{
int integer;
};
int main() {
Foo f;
int integer;
f.integer;
}
using
is of course not a simple replacement for #define integer int
, but what makes this code apparently well-formed while int int;
would make it ill-formed?
While it's a little surprising that one can access a name declared in an outer scope, and then later hide that name , it's just a straightforward application of the rules for scoping:
In turn, the point of a name's declaration is:
So when you do
integer integer
, you haven't yet declared the block-scoped nameinteger
, which means you can still see the globalinteger
. This also means you cannot dointeger integer = (integer)0
.It's easier to explain why
int int
won't compile.int
is a keyword, so no syntactic rule which could declare it as a name would be able to; it doesn't match the rules for "what a name looks like".Because
int
is a keyword, it cannot be an identifier, which means it cannot be a name.