Reputation: 42335
namespace A
{
int a = 1;
int x = 2;
}
namespace B
{
int b = 3;
int x = 4;
}
using namespace A;
using namespace B;
using B::x;
int main()
{
return x; // error : reference to 'x' is ambiguous
}
How to hide A::x
and expose B::x
only in such a case?
Upvotes: 0
Views: 50
Reputation: 234715
using namespace
brings that namespace into the current one.
Once you've done that you need to deal with any ambiguities yourself. Normally you use the scope resolution operator for that.
C++ doesn't give you the ability to un-bring in a namespace.
The best bet, by a country mile, is to avoid using namespace
in the first place. Learn to love code containing lots of ::
operators.
Upvotes: 1
Reputation: 385144
You can't.
You brought both names into scope, and that's that.
To fix that, don't do that; avoid using namespace
.
Upvotes: 2