The following source code compiles correctly with Visual Studio 2010:
namespace NS
{
class X
{
};
X Y(X str);
}
void myFun()
{
NS::X x;
Y(x);
}
(this is reduced code where all my own class and function names have been replaced by X, Y, …)
I would expect that the line Y(x) would fail to compile, since it should be NS::Y(x).
This source code is compiled with the following command
cl /c file.cpp
There are no other files included here, no other command line options.
Why does this file compile?
Bug in VS2010? Or something that I (and my 3 other colleagues) overlooked?
What you are experiencing is due to
ADL(Argument Dependent Lookup).There is nothing wrong with your snippet (besides the fact that the linker will probably complain about
NS::Yisn’t defined), but it should compile – VS2012 is handling the snippet as it should.The compiler will find
NS::Ydue to the fact that the type of parameterx(NS::X) is in the appropriate scope.