Mirror of the gdb mailing list
 help / color / mirror / Atom feed
From: Gabriel Dos Reis <gdr@codesourcery.com>
To: Jim Blandy <jimb@redhat.com>
Cc: gdb@sources.redhat.com
Subject: Re: C++ namespace using directives
Date: Tue, 16 Apr 2002 19:39:00 -0000	[thread overview]
Message-ID: <fl7kn7dp59.fsf@jambon.cmla.ens-cachan.fr> (raw)
In-Reply-To: Jim Blandy's message of "Tue, 16 Apr 2002 16:41:01 -0500 (EST)"

Jim Blandy <jimb@redhat.com> 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


  reply	other threads:[~2002-04-17  2:39 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-04-16 14:41 Jim Blandy
2002-04-16 19:39 ` Gabriel Dos Reis [this message]
2002-04-22 14:52 ` Don Howard
2002-04-29 13:28   ` Jim Blandy
2002-04-29 14:30     ` Don Howard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=fl7kn7dp59.fsf@jambon.cmla.ens-cachan.fr \
    --to=gdr@codesourcery.com \
    --cc=gdb@sources.redhat.com \
    --cc=jimb@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox