This is the mail archive of the gdb@sources.redhat.com mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: C++ namespace using directives



Don Howard <dhoward@redhat.com> writes:
> On Tue, 16 Apr 2002, Jim Blandy wrote:
> > Could a C++ person check my understanding of `using namespace'
> > directives?
> > 
> > I'm reading Stroustrup, and the more I read about `using namespace',
> > the weirder it gets.  Check this out:
> > 
> >     namespace A
> >     {
> >       int x;
> >       int y;
> >       int z;
> >     };
> > 
> >     void f ()
> >     {
> >       int *x;
> > 
> >       {
> >         int *y;
> >         using namespace A;
> > 
> >         *x;  /* `x' refers to local x, not A::x */
> >         *y;  /* `y` refers to local y, not A::y */
> >         z;  /* `z' refers to A::z */ 
> >       }
> >     }
> 
> This example seems correct to me, as the compiler can dis-ambiguate based 
> on type-- dereference works on pointers, so x and y must refer to the 
> local versions.

No, that's not what's going on at all.  In Stroustrup's example, there
is no dereferencing going on, and he makes the same claims about which
binding each reference refers to as I do.  I added the dereferences as
a sanity check, to make sure GCC was doing the right thing.

So you see why I think this behavior is way wacky?  The `using
namespace' directive appears in the inner block, the local definition
appears in the outer block, but references to `x' in the inner block
still see the local definition in the outer block.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]