From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 24473 invoked by alias); 17 Apr 2002 02:39:55 -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 24430 invoked from network); 17 Apr 2002 02:39:50 -0000 Received: from unknown (HELO ariane.ens-cachan.fr) (138.231.176.4) by sources.redhat.com with SMTP; 17 Apr 2002 02:39:50 -0000 Received: from mayo.cmla.ens-cachan.fr (mayo.cmla.ens-cachan.fr [138.231.64.2]) by ariane.ens-cachan.fr (8.12.2/jtpda-5.3.3) with ESMTP id g3H2dmRp015104 ; Wed, 17 Apr 2002 04:39:49 +0200 Received: from jambon.cmla.ens-cachan.fr (jambon.cmla.ens-cachan.fr [138.231.64.60]) by mayo.cmla.ens-cachan.fr (8.9.1a/jtpda-5.3.2) with ESMTP id EAA06843 ; Wed, 17 Apr 2002 04:39:47 +0200 (MET DST) Received: from (dosreis@localhost) by jambon.cmla.ens-cachan.fr (8.9.3/jtpda-5.3.1/CL) id EAA27487 ; Wed, 17 Apr 2002 04:39:46 +0200 (MET DST) To: Jim Blandy Cc: gdb@sources.redhat.com Subject: Re: C++ namespace using directives References: <20020416214101.C985E5EA11@zwingli.cygnus.com> From: Gabriel Dos Reis In-Reply-To: Jim Blandy's message of "Tue, 16 Apr 2002 16:41:01 -0500 (EST)" Organization: CodeSourcery, LLC Mime-Version: 1.0 (generated by tm-edit 7.106) Content-Type: text/plain; charset=US-ASCII Date: Tue, 16 Apr 2002 19:39:00 -0000 Message-ID: X-SW-Source: 2002-04/txt/msg00280.txt.bz2 Jim Blandy writes: | 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 program is type-correct, so you can see exactly which definitions | those identifiers refer to. I ran it through the newest GCC, and it | didn't complain about ambiguities. Stroustrup C.10.1 agrees. Yes. That matches the standard requirement: 3.4.1/2 The declarations from the namespace nominated by a using-directive become visible in a namespace enclosing the using-directive; see 7.3.4. For the purpose of the unqualified name lookup rules described in 3.4.1, the declarations from the namespace nominated by the using-directive are considered members of that enclosing namespace. So in the above program, after the 'using namespace A', A::x and A::y are considered (for the name lookup process) as members of the global namespace. So they are hidden by the local definitions in f(). | Weird, huh? Although the `using namespace' directive does make A's | variables visible, A's bindings are still *shadowed* by local | variables in blocks that *enclose* the one containing the `using | namespace' directive. Exact. | So, here's the way I'd describe the effect of a `using namespace' | directive: | | To look up some identifier X in a scope that has a `using namespace N' | directive, search for both X and N::X. In the transitive closure of the enclosing namespace. [...] | Regarding what constitutes an "ambiguity": if the same declaration | makes it into a scope under two different names, that's not considered | an ambiguity. Right. [...] | However, if we give namespace B its own `int x' definition, the | compiler does complain. Right, | Is this all correct? Yes. -- Gaby