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.