Search code examples
c++c++11namespacesstandardsname-lookup

How to hide `A::x` and expose `B::x` only in such a case?


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?


Solution

  • You can't.

    You brought both names into scope, and that's that.

    To fix that, don't do that; avoid using namespace.