https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80288
Bug ID: 80288
Summary: Using directive injects names into a wrong namespace
Product: gcc
Version: 7.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: bugger_gcc at interia dot pl
Target Milestone: ---
Consider the following code:
namespace N1 {
int i;
namespace N2 {
int i;
}
void f();
}
void N1::f()
{
using namespace N2;
i = 0;
}
GCC complains:
<source>: In function 'void N1::f()':
<source>:12:5: error: reference to 'i' is ambiguous
Apparently the using directive puts N2::i into N1, causing the conflict with
N1::i. However, N2::i should be put into the global namespace and hidden by
N1::i, thus there should be no ambiguity.
According to 7.3.4/2:
"[...] the names appear as if they were declared in the nearest enclosing
namespace which contains both the using-directive and the nominated namespace.
[Note: In this context, “contains” means “contains directly or indirectly”.
—end note]"
The nearest enclosing namespace of the using directive in this case is the
global namespace, not N1, because N1::f() is defined in the global namespace
and IIUC the fact that it's a member of N1 only affects name lookup within its
body, not the containment of the using directive.
This is how Clang interprets this too: https://godbolt.org/g/LMqveL