Topic: nested namespaces and using directive
Author: deadimp@gmail.com
Date: Tue, 2 Jan 2007 12:18:35 CST Raw View
Whenever you declare a using directive, it's allowing implicit access
to the namespace's scope, it doesn't let you define namespaces in the
namespace using'd unless you explicitly do so.
Here, you seem to be thinking that since you have access to the 'base'
namespace, you can declare items inside it's namespace 'sub' without
explicitly stating so.
eric_backus@alum.mit.edu wrote:
> namespace uses_base
> {
> using namespace base;
>
> void uses_base_func()
> {
> base_func(); // OK
> uses_base::base_func(); // OK
> }
>
> namespace sub
> {
> void uses_base_sub_func()
> {
> base_sub_func(); // error *
> base::sub::base_sub_func(); // OK
> uses_base::sub::base_sub_func(); // error ***
> }
> }
> }
* You're thinking that you have access to "base::sub" by declaring
"namespace sub" contained by "namespace uses_base", but you don't. You
actually only have access to "uses_base::sub". You'd have to explicity
state "sub" (not sure) or "base::sub".
*** You're thinking that you've declared "uses_base_sub_func()" in
"base::sub" since you've declared the using directive, however, it's
declaring it in "uses_base::sub", which isn't the same as "base::sub".
Therefore, "base::sub::uses_base_sub_func()" does not exists, but
"uses_base::sub::uses_base_sub_func()" does.
Tried to make it clear. Hope that helped.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "Manfred von Willich" <manfred@techniroot.co.za>
Date: Tue, 2 Jan 2007 12:22:52 CST Raw View
eric_backus@alum.mit.edu wrote:
> > You are expecting the function foo to be able to access the variable
> > Y::var2 without qualifying it, which can not be, because you have not
> > said which subspace to look for the variable. Is your example
> > different than this code above in any fundamental way?
>
> Yes, I believe my example is fundamentally different. My example has a
> "using directive" which should (in my naive opinion) make the inner
> namespace accessable.
You do appear to be making at least the mistake alluded to in the
previous reply (there may be other issues that I have not looked at
closely).
The short of it is that you seem to be under the misconception that
using namespace base;
has the effect that
using namespace base::sub;
has: the latter allows you to see what is inside base::sub without
using qualification; the former does not.
Manfred
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "Ivan Novick" <ivan@0x4849.net>
Date: Tue, 2 Jan 2007 12:21:47 CST Raw View
eric_backus@alum.mit.edu wrote:
> Yes, I believe my example is fundamentally different. My example has a
> "using directive" which should (in my naive opinion) make the inner
> namespace accessable.
Ok, well the best quote i can find from the standard is 7.3.4/1: "A
using-directive specifies that the names in the nominated namespace can
be used in the scope in which the using-directive appears after the
using-directive". There is no mention that names in inner namespaces
will also be nominated. Testing a very simple example we can see what
the gcc compiler thinks the behavior should be:
namespace X
{
namespace Y
{
int a;
}
}
int main()
{
using namespace X;
// this doesn't compile
a = 0;
return 0;
}
----
Ivan
http:/www.0x4849.net
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "eric_backus@alum.mit.edu" <eric_backus@alum.mit.edu>
Date: Tue, 2 Jan 2007 18:31:04 CST Raw View
deadimp@gmail.com wrote:
> eric_backus@alum.mit.edu wrote:
> > namespace uses_base
> > {
> > using namespace base;
> >
> > void uses_base_func()
> > {
> > base_func(); // OK
> > uses_base::base_func(); // OK
> > }
> >
> > namespace sub
> > {
> > void uses_base_sub_func()
> > {
> > base_sub_func(); // error *
> > base::sub::base_sub_func(); // OK
> > uses_base::sub::base_sub_func(); // error ***
> > }
> > }
> > }
> * You're thinking that you have access to "base::sub" by declaring
> "namespace sub" contained by "namespace uses_base", but you don't. You
> actually only have access to "uses_base::sub". You'd have to explicity
> state "sub" (not sure) or "base::sub".
>
> *** You're thinking that you've declared "uses_base_sub_func()" in
> "base::sub" since you've declared the using directive, however, it's
> declaring it in "uses_base::sub", which isn't the same as "base::sub".
That seems to be the crux of the issue. uses_base::sub is not the same
as base::sub, even though there's a "using namespace base" inside of
uses_base. This is counter-intuitive to me.
After some thought, it seems to me to act somewhat like class
inheritance does--if you have the same function name in the base class
and the derived class, they don't become overloads of each other,
instead the derived class function hides the base class function. In
this case, the base::sub and uses_base::sub don't get conglomerated
together, instead uses_base::sub hides base::sub.
> Therefore, "base::sub::uses_base_sub_func()" does not exists, but
> "uses_base::sub::uses_base_sub_func()" does.
>
> Tried to make it clear. Hope that helped.
Thank you. What you've said is clear, and this behavior is what I'm
getting from several different compilers. I'm still not clear why this
behavior is desirable, but it's enough of a corner case that perhaps it
doesn't matter.
--
Eric
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "eric_backus@alum.mit.edu" <eric_backus@alum.mit.edu>
Date: Sat, 30 Dec 2006 12:19:06 CST Raw View
I'm seeing behavior from nested namespaces and a using directive that
seems wrong to me, but I've tried this code on several compilers and
all agree.
I've got a nested namespace, base and base::sub. And I've got another
nested namespace uses_base and uses_base::sub. In the uses_base
namespace, I have "using namespace base", in an attempt to make
uses_base look something like a superset of base. This works as
expected for names directly within the base and uses_base namespaces,
but not for names within the base::sub namespace. Here's the code:
namespace base
{
void base_func() {}
namespace sub
{
void base_sub_func() {}
}
namespace sub
{
void base_sub_func2()
{
base_sub_func(); // OK
}
}
}
namespace uses_base
{
using namespace base;
void uses_base_func()
{
base_func(); // OK
uses_base::base_func(); // OK
}
namespace sub
{
void uses_base_sub_func()
{
base_sub_func(); // error
base::sub::base_sub_func(); // OK
uses_base::sub::base_sub_func(); // error
}
}
}
int
main()
{
base::base_func(); // OK
uses_base::base_func(); // OK
base::sub::base_sub_func(); // OK
uses_base::sub::base_sub_func(); // error
uses_base::sub::uses_base_sub_func(); // OK
return 0;
}
The lines marked error are lines that I thought should compile
successfully, but do not on the compilers that I've tried. It appears
that the lookup of namespace names is different than the lookup of
function names (uses_base::base_func() works, but
uses_base::sub::base_sub_func() does not). This seems unnecessarily
restrictive, and seems like a gratuitous difference.
What does the standard say about this? Why would this be desirable
behavior?
--
Eric Backus
eric_backus@alum.mit.edu
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "Ivan Novick" <ivan@0x4849.net>
Date: Sun, 31 Dec 2006 16:56:57 CST Raw View
eric_backus@alum.mit.edu wrote:
> namespace base
> {
> void base_func() {}
>
> namespace sub
> {
> void base_sub_func() {}
> }
> namespace sub
> {
> void base_sub_func2()
> {
> base_sub_func(); // OK
> }
> }
> }
>
> namespace uses_base
> {
> using namespace base;
>
> void uses_base_func()
> {
> base_func(); // OK
> uses_base::base_func(); // OK
> }
>
> namespace sub
> {
> void uses_base_sub_func()
> {
> base_sub_func(); // error
> base::sub::base_sub_func(); // OK
> uses_base::sub::base_sub_func(); // error
> }
> }
> }
>
> int
> main()
> {
> base::base_func(); // OK
> uses_base::base_func(); // OK
>
> base::sub::base_sub_func(); // OK
> uses_base::sub::base_sub_func(); // error
> uses_base::sub::uses_base_sub_func(); // OK
>
> return 0;
> }
>
>
>
> The lines marked error are lines that I thought should compile
> successfully, but do not on the compilers that I've tried. It appears
> that the lookup of namespace names is different than the lookup of
> function names (uses_base::base_func() works, but
> uses_base::sub::base_sub_func() does not). This seems unnecessarily
> restrictive, and seems like a gratuitous difference.
>
> What does the standard say about this? Why would this be desirable
> behavior?
AFAICT, the behavior you are describing is equivalent to this simpler
code:
namespace X
{
int var1;
namespace Y
{
int var2;
}
void foo()
{
var1 = 0;
var2 = 0;
}
}
You are expecting the function foo to be able to access the variable
Y::var2 without qualifying it, which can not be, because you have not
said which subspace to look for the variable. Is your example
different than this code above in any fundamental way?
Thanks,
----
Ivan
http://www.0x4849.net
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "eric_backus@alum.mit.edu" <eric_backus@alum.mit.edu>
Date: Mon, 1 Jan 2007 03:08:47 CST Raw View
Ivan Novick wrote:
> AFAICT, the behavior you are describing is equivalent to this simpler
> code:
>
> namespace X
> {
> int var1;
> namespace Y
> {
> int var2;
> }
>
> void foo()
> {
> var1 = 0;
> var2 = 0;
> }
> }
>
> You are expecting the function foo to be able to access the variable
> Y::var2 without qualifying it, which can not be, because you have not
> said which subspace to look for the variable. Is your example
> different than this code above in any fundamental way?
Yes, I believe my example is fundamentally different. My example has a
"using directive" which should (in my naive opinion) make the inner
namespace accessable.
--
Eric Backus
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]