From: Don Howard <dhoward@redhat.com>
To: Jim Blandy <jimb@redhat.com>
Cc: <gdb@sources.redhat.com>
Subject: Re: C++ namespace using directives
Date: Mon, 29 Apr 2002 14:30:00 -0000 [thread overview]
Message-ID: <Pine.LNX.4.33.0204291330370.23254-100000@theotherone.redhat-remotie.org> (raw)
In-Reply-To: <npznzm8d2d.fsf@zwingli.cygnus.com>
On 29 Apr 2002, Jim Blandy wrote:
>
> 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 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
prev parent reply other threads:[~2002-04-29 21:30 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
2002-04-22 14:52 ` Don Howard
2002-04-29 13:28 ` Jim Blandy
2002-04-29 14:30 ` Don Howard [this message]
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=Pine.LNX.4.33.0204291330370.23254-100000@theotherone.redhat-remotie.org \
--to=dhoward@redhat.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