Name lookup inconsitency

36 views
Skip to first unread message

Anders Granlund

unread,
Jul 18, 2015, 7:06:15 AM7/18/15
to std-dis...@isocpp.org
Hi,

I'm trying to understand why this program does not give an name-lookup ambiguity for i;

namespace X { int i = 1; }

namespace Q {    
    namespace P {        
        int i = 2;
        using namespace X;
    }

    using namespace P;
    
    int l = i;
}
    
int main() {}

If me modify it like this we get a name-lookup ambiguity:

namespace X { int i = 1; }
    
namespace P {        
    int i = 2;
    using namespace X;
}

using namespace P;
    
int l = i;
    
int main() {}

The only change I made here is to remove the namespace Q and place it's content in the global namespace instead.

I have tried with 3 different compilers ( GCC and Clang with http://melpon.org/wandbox and visual c++ with http://webcompiler.cloudapp.net/ ). The all give the results stated in this email, and i'm trying to find out why.

Can anyone explain the behaviour in terms of the c++ standard? I fail to understand it.

David Krauss

unread,
Jul 18, 2015, 7:24:52 AM7/18/15
to std-dis...@isocpp.org

On 2015–07–18, at 7:06 PM, Anders Granlund <anders.g...@gmail.com> wrote:

Can anyone explain the behaviour in terms of the c++ standard? I fail to understand it.

Using-directives make names visible as if declared in the nearest namespace enclosing both the directive and the declaration. In the first example, Q::P::i appears to be in namespace Q and X::i appears to be int the global namespace. Unqualified lookup inside Q only finds Q::P::i. In the second example, both declarations are equally visible at the global namespace level, leading to ambiguity.

Anders Granlund

unread,
Jul 18, 2015, 8:42:56 AM7/18/15
to std-dis...@isocpp.org
Ok. Thanks! That explains things.

I guess I found the part in the standard that you are talking about (searched for "nearest enclosing namespace"):


--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussio...@isocpp.org.
To post to this group, send email to std-dis...@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-discussion/.

Anders Granlund

unread,
Jul 18, 2015, 11:31:56 AM7/18/15
to std-dis...@isocpp.org
I'm a bit confused about the implications of [namespace.udir]p2. Consider the following program:

namespace X {
    int i = 1;
}

namespace Y {
    namespace Z {
        using namespace X;
    }
}

int main() { i; }

In it name-lookup for i in main fails. This makes sense, but it doesn't seem to agree with  [namespace.udir]p2 ( http://eel.is/c++draft/dcl.dcl#namespace.udir-2 ).

According to [namespace.udir]p2 shouldn't unqualified name-lookup behave as if the definition int i = 1; was  made in the global scope? And therefore shouldn't the name-lookup for i in main  succeed?

Anders Granlund

unread,
Jul 18, 2015, 3:53:29 PM7/18/15
to std-dis...@isocpp.org
Nevermind, [namespace.udir]p2 only applies to unqualified name-lookup inside Z, so no contradiction in my previous mail.
Reply all
Reply to author
Forward
0 new messages