From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 15279 invoked by alias); 29 Apr 2002 21:30:52 -0000 Mailing-List: contact gdb-help@sources.redhat.com; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-owner@sources.redhat.com Received: (qmail 15252 invoked from network); 29 Apr 2002 21:30:49 -0000 Received: from unknown (HELO cygnus.com) (205.180.83.203) by sources.redhat.com with SMTP; 29 Apr 2002 21:30:49 -0000 Received: from theotherone.redhat-remotie.org (romulus.sfbay.redhat.com [172.16.27.251]) by runyon.cygnus.com (8.8.7-cygnus/8.8.7) with ESMTP id OAA15284; Mon, 29 Apr 2002 14:30:45 -0700 (PDT) Received: from localhost (localhost.fidalgo.net [127.0.0.1]) by theotherone.redhat-remotie.org (Postfix) with ESMTP id AB6C2BB260; Mon, 29 Apr 2002 14:25:17 -0700 (PDT) Date: Mon, 29 Apr 2002 14:30:00 -0000 From: Don Howard X-X-Sender: To: Jim Blandy Cc: Subject: Re: C++ namespace using directives In-Reply-To: Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-SW-Source: 2002-04/txt/msg00495.txt.bz2 On 29 Apr 2002, Jim Blandy wrote: > > Don Howard 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 the Stroustrup example looks like this: namespace A { int x = 10; int y = 20; int z = 30; }; int x = 100; void f () { int x = 0; { int y = 0; using namespace A; x = 1; y = 2; z = 42; // Which one is not like the others... This one. printf ("A::x == %d\nA::y == %d\nA::z == %d\nx == %d\ny == %d\n::x == %d\n", A::x, A::y, A::z, x, y, ::x); } } int main (void) { f (); } .... franklinstower$ ./foo A::x == 10 A::y == 20 A::z == 42 x == 1 y == 2 ::x == 100 > > 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. Yes, I expected the 'using namespace' directive to affect scoping rules somehow, but that isn't what it does. Hmm. reading Stroustrup's reasoning makes this more clear. He wanted to implement a backward compatable way to partition namespaces -- one that would not require existing code to be changed. To that end, the 'using namespace' directive is not a scope resolution operator. The statement 'using namespace A' just makes the members of A available to the local block at global (?) scope (does that make sense?). So the following doesn't compile due to ambiguity: namespace A { ... int z; } int z; ... { using namespace A; z = 42; } [And it I just found Gaby Dos Reis' response to your first mail, which says just what I spent 30 minutes looking up...] -- dhoward@redhat.com gdb engineering