typedef name question
David Fang
fang@csl.cornell.edu
Wed Jan 31 00:20:00 GMT 2007
> > You are allowed to introduce identifiers in a scope that
> > shadow identifiers outside that scope. Even in the
> > "foo foo;" case, where you are using an outer scope
> > identifier and in the process are introducing a new
> > identifier that shadows that self-same outer
> > scope identifier.
>
> Just a small add-on:
>
> In C++ you can easily test this rule and still access the outer identifier
> by explicitly looking it up in global namespace scope. The following
> compiles with g++:
>
> // foo is defined in the global namespace
> typedef int foo;
>
> foo foo_fct(int a) {
> foo foo; /* line 3: a variable foo with type foo */
> return (::foo)foo; /* line 4: cast to outer foo */
> }
>
> Of course, this does not work with C.
One more add-on:
If you want to detect this sort of shadow-ing (as it can often reveal
programmer errors), add -Wshadow as a compile flag. To reject it
outright, add -Werror. (In gcc-4.2+, you'll be able to select which
warnings you want to treat as errors.)
Fang
More information about the Gcc-help
mailing list