How do names get resolved in C++ namespace?
I'm especially interested in cases where a name is provided in multiple different ways, for instance by a parent namespace and a using namespace.
Take this piece of code:
namespace ns1 {
static auto str = "ns1::str";
}
namespace ns2 {
static auto str = "ns2::str";
namespace sub {
using namespace ns1;
auto f() { return str; }
}
}
With my compiler, ns2::sub::f() returns "ns2::str". I expected it to return "ns1::str", because using namespace ns1 appears in ns2::sub.
And now this piece:
static auto str = "str";
namespace ns1 {
static auto str = "ns1::str";
}
namespace ns2 {
using namespace ns1;
auto f() { return str; }
}
I expected it to behave like the previous case. Instead it doesn't compile:
error: reference to ‘str’ is ambiguous
What's the logic behind this?