This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: typedef name question
- From: David Fang <fang at csl dot cornell dot edu>
- To: Daniel Lohmann <daniel dot lohmann at informatik dot uni-erlangen dot de>
- Cc: Michael Gong <mwgong at cs dot utoronto dot ca>, <gcc-help at gcc dot gnu dot org>
- Date: Tue, 30 Jan 2007 19:20:05 -0500 (EST)
- Subject: Re: typedef name question
> > 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