2

In the following program we have two namespaces:

#include <iostream>
namespace B
{
    int c = 42;
}

namespace A
{
    using namespace B;
    int a = 442;
}

namespace B
{
    int b = 24;
}

int main()
{ 
    std::cout << A::a << std::endl; //442
    std::cout << A::b << std::endl; //24
    std::cout << A::c << std::endl; //42
}

DEMO

I thought the behavior of the program is being covered by N4296::3.3.6/1 [basic.scope.namespace]:

A namespace member name has namespace scope. Its potential scope includes its namespace from the name’s point of declaration (3.3.2) onwards; and for each using-directive (7.3.4) that nominates the member’s namespace, the member’s potential scope includes that portion of the potential scope of the using-directive that follows the member’s point of declaration.

So, in the case of the namespace A, the potential scope of the member b shouldn't have included any portion of the program, because the member was declared later then the using directive. But actually it can be found by qualified name lookup. What's wrong?

1 Answer 1

2

If you read this fragment again:

the member’s [b's] potential scope includes that portion of the potential scope of the using-directive [in A] that follows the member’s point of declaration.

I believe you have to read it as stating that b is in the scope of A from the point of b's declaration onward. Where you print A::b, that does in fact "follow the member's point of declaration", so for that line, b can be found in the scope of A. This is perfectly valid.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I'd also say that it's an qualified-name-lookup issue. Covered by 3.4.3.2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.