Re: Using directive/declaration and the global namespace
Alex Blekhman wrote:
Actually, it's strange that the code compiles at all. According to 7.3.4/4:
7.3.4/4 Using directive
"If name lookup finds a declaration for a name in two different
namespaces, and the declarations do not declare the same entity and do
not declare functions, the use of the name is ill-formed."
So, when resolving name `A; within function `f' compiler should have
following set of immediately visible names:
- N2::A (from the `using' declaration)
- N2::A (from the `using' directive)
- N1::A (from the `using' directive)
And compiler shows exactly these names in error message for global scope
case. It seems that for some obscure reason, the `using N2::A'
declaration, when applied within namespace `N3', somehow hides two other
alternatives and has higer precedence in name lookup.
Thanks Alex:
Don't know about that, but if you look at Stroudstrup, 3rd edition, page
181, he gives an example which (I think) is exactly what I did with the
namespace N3, saying that the using declaration disabiguates the
duplicated class in the two directives. So I was very puzzled when it
did not work for me, and it took me a while to realize that it was
because the difference was that I was trying to do it in the global
namespace.
<aside>
This came up because, still using VC6, I had written my own versions of
CA2W etc, modeled after the ones in VC7.1, but correcting a bug therein
and adding things like CU2W, where U is UTF-8. I did not include
afxconv.h and all was good. But when I compiled this code on VC8 I got
name collisions, because atlbase.h (which I WAS using) seems to include
afxconv.h in VC8 (and also puts things in the ATL namespace). So I was
trying to disambiguate these things. Then I discovered that you can
define _ATL_NO_AUTOMATIC_NAMESPACE to avoid this problem. But I
remained, and remain, confused.
</aside>
David Wilkinson