Search code examples
c++visual-c++lambdac++11argument-dependent-lookup

ADL fails when there are lambda arguments?


quite some time ago i noticed that in Visual C++ 10 ADL fails when at least one of the arguments is a lambda.

std::vector<float> vec;
for_each(begin(vec), end(vec), [](float) {}); 

The above fails to compile on VC++10 and 11 (beta) (begin and end are found via ADL). When i convert the lambda function into a regular free function things work just as expected.

I've asked on Herb Sutters blog once and also read some posts on msdn connect and the usual answers were: this is a bug, we havent implemented the latest standard of the lambdas yet which - at that time - was quite understandable. Things haven't been in a baked form yet. On MS connect there have also been disturbing comments that this will not be resolved for the next release i.e. vc 11.

My question is, is this code expected to work under the C++11 standard? I cant quite figure that out. Do i really have to prefix my for_each and other algorithms with std:: when I'm using lambdas? I somehow suspect that this behavior will not change after vc++11 release.


Solution

  • That is perfectly valid code. Any bug-free compiler will be able to compile it. But since MSVC has bug and so is unable to search the function through ADL, then maybe you should not rely on ADL and instead qualify it with std:: helping the compiler to find the function.