Name ambiguity

I am trying to understand why this program does not give ambiguity to the name lookup 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 we change it like this, we get the ambiguity of the search:

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 Q namespace and put its contents in the global namespace.

I tried with three different compilers:

All give the results indicated in this letter, and I'm trying to find out why.

Can someone explain the behavior in terms of the C ++ standard? I do not understand this.

+4
source share
1 answer

i P, using

using namespace X;

X ( X P). , i P ( Q - ) X::i .

++ (3.4.1 )

2 , using , using; 7.3.4.

,

namespace X { int i = 1; }

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

    using namespace P; // 2

    int l = i;
}

# 1 , β„– 2 - Q.

i -

//...
using namespace X;
//...
using namespace P;
+1

Source: https://habr.com/ru/post/1598501/


All Articles