From cppreference :
From the point of view of unqualified name lookup of any name after a using-directive and until the end of the scope in which it appears, every name from namespace-name is visible as if it were declared in the nearest enclosing namespace which contains both the using-directive and namespace-name.
The "inner-most" scope that contains namespace-name is the scope in which namespace-name is declared, and it must enclose the scope containing the using-directive for the using-directive to be valid.
This implies that "the nearest enclosing namespace which contains both the using-directive and namespace-name" is just the namespace in which namespace-name has been declared. In that case, why phrase it in such a verbose manner?
Am I misunderstanding something? Are there any subtleties here that I'm missing?
is just the namespace in which namespace-name has been declared. In that case, why phrase it in such a verbose manner?
That is not always the case. To make it clear, consider the following contrived example. Here, the nearest enclosing namespace that contains both the using-directive and the namespace-name is the global namespace. This means that int k = i;
is ambiguous because there are two i
that can be used.
If on the other hand, if we were to follow your modified rule(just the namespace in which namespace-name has been declared), then the concerning namespace would be only outer
that only contains the namespace-name.
int i = 12;
namespace outer
{
namespace n1
{
int i = 10;
}
}
namespace n2
{
void f()
{
using namespace outer::n1;
int k = i; //this is ambiguous because i already exists in global namespace
}
}