4

To my astonishment the following code compiles and prints "X" on VC++ 2017:

#include <string>
#include <iostream>


namespace A {
    using namespace std;
}

namespace B {
    using namespace A;
}

namespace C {
    using namespace B;
    string a;
}

int main()
{
    C::a = "X";
    std::cout << C::a;
    return 0;
}

It looks like the using namespace std works from namespace A through namespace B into namespace C.

Is this a bug in Visual C++ or does it concur with the language specification?

I had expected that using namespace std ends at the end of the enclosing scope which is at the end of the definition of namespace A.

EDIT: I understand that the accepted answer to this question also answers my question. But that post is more about anonymous namespaces, while this one is about the transitivity of the using namespace directive. So I think it s a better example and the question makes sense.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Kit Fisto
  • 4,385
  • 5
  • 26
  • 43
  • 3
    @LogicStuff: My question is similar but not the same. It has nothing to do with anonymous namespaces and others that search for the problem won't find that answer like I did not. – Kit Fisto Feb 16 '18 at 16:05
  • 1
    Why souldn't it work exactly? `using namespace` imports all names into the host namespace, they don't vanish afterwards. – Quentin Feb 16 '18 at 16:06
  • @KitFisto It is a subquestion of the linked question, answered in the first sentence of the accepted answer. What is the problem? – LogicStuff Feb 16 '18 at 16:36
  • @LogicStuff: It is true that one of the answers to that other question also answers my question. But the questions themselves are different as far as I see it. – Kit Fisto Feb 16 '18 at 16:44
  • @KitFisto: Despite the name, "duplicate" is often used when the answers match, as long as the question is on sufficiently the same topic. It's a balancing act but there is no point in repeating answers just because the question differs slightly. Read "here's a duplicate" as "your answer may be found here". That all being said, I would _not_ consider these questions to be sufficiently similar for that to be a dupe. – Lightness Races in Orbit Feb 16 '18 at 17:44

1 Answers1

2

Yes:

[C++14: 7.3.4/4]: For unqualified lookup (3.4.1), the using-directive is transitive: if a scope contains a using-directive that nominates a second namespace that itself contains using-directives, the effect is as if the using-directives from the second namespace also appeared in the first. [..]

So, the compiler is correct; using namespace here effectively imports the names into the enclosing namespace:

[C++14: 7.3.4/2]: A using-directive specifies that the names in the nominated namespace can be used in the scope in which the using-directive appears after the using-directive. [..]

The "scope" here is that of the namespace; the contents of a scope doesn't vanish just because a } is encountered. Familiarity with block scopes sometimes makes it feel that way, though.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055