Visual Studio Fails At Namespace Lookups
I recently ran into an interesting Visual Studio bug. Namely, if you have nested namespaces and are making us of a using statement, it seems to mess up namespace lookup.
namespace A {
struct X {};
namespace B {
struct Y {
void fun (X &);
};
}
}
using namespace A::B;
void Y::fun(X &) {}
int main () {
A::B::Y y;
}This code compiles fine with clang and gcc, but for whatever reason, doesn’t compile under Visual Studio. You’ll see errors similiar to:
missing type specifier - int assumed. Note: C++ does not support default-int
syntax error: missing ',' before '&'
'void A::B::Y::fun(X &)': overloaded member function not found in 'A::B::Y'It seems to overwrite the root namespace scoping (in this case, A, making A::X not found). This is fixable by explicitly writing all namespaces like such:
namespace A {
struct X {};
namespace B {
struct Y {
void fun (X);
};
}
}
void A::B::Y::fun(X) {}
int main () {
A::B::Y y;
}However, this can be a huge pain if you have a lot of references and can really clutter your code. Fortunately, there’s another easy fix, redeclare the root namespace:
using namespace A;And that should fix it. Thanks to those over at StackOverflow who helped me pinpoint this issue. I’ve also submitting a bug for the Visual Studio folks.