Calling a bound lambda through a std::function variable
-
Thursday, October 28, 2010 7:30 PM
I am unsucessfully trying to compile the following code in Visual Studio 2010. For what it is worth, the code compiles fine in GCC 4.5.1 with the -std=c++0x flag. Can someone help me figure out why this is not working?
#include <functional> #include <iostream> #include <string> int main() { int value = 42; auto myBoundLambda = [&](const std::string& header) -> void { std::cout << header << value << std::endl; }; std::function<void()> myFunction = std::bind(myBoundLambda, "Answer to life is: "); myFunction(); return 0; }
Error 1 error C2903: 'result' : symbol is neither a class template nor a function template c:\program files (x86)\microsoft visual studio 10.0\vc\include\xxresult 28 1 CPP0X Lambda Test
Error 2 error C2039: 'result' : is not a member of '`anonymous-namespace'::<lambda0>' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xxresult 28 1 CPP0X Lambda Test
Error 3 error C2143: syntax error : missing ';' before '<' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xxresult 28 1 CPP0X Lambda Test
Error 4 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\program files (x86)\microsoft visual studio 10.0\vc\include\xxresult 28 1 CPP0X Lambda Test
Error 5 error C2039: 'type' : is not a member of '`global namespace'' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xxresult 28 1 CPP0X Lambda Test
Error 6 error C2238: unexpected token(s) preceding ';' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xxresult 28 1 CPP0X Lambda Test
Error 7 error C2039: '_Type' : is not a member of 'std::tr1::_Result_type1<__formal,_Fty,_Arg0>' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xxresult 40 1 CPP0X Lambda Test
Error 8 error C2146: syntax error : missing ';' before identifier '_Type' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xxresult 40 1 CPP0X Lambda Test
Error 9 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\program files (x86)\microsoft visual studio 10.0\vc\include\xxresult 40 1 CPP0X Lambda Test
Error 10 error C2602: 'std::tr1::_Result_of1<_Fty,_Farg0>::_Type' is not a member of a base class of 'std::tr1::_Result_of1<_Fty,_Farg0>' c:\program files (x86)\microsoft visual studio 10.0\vc\include\xxresult 40 1 CPP0X Lambda Test
Error 11 error C2868: 'std::tr1::_Result_of1<_Fty,_Farg0>::_Type' : illegal syntax for using-declaration; expected qualified-name c:\program files (x86)\microsoft visual studio 10.0\vc\include\xxresult 40 1 CPP0X Lambda Test
All Replies
-
Thursday, October 28, 2010 8:25 PM
As far as I can tell, this is a deficiency in VC++ 2010's standard library implementation, rather than a bug in VC++ 2010's lambda implementation. By design, C++0x lambdas do not implement the TR1 result_of protocol, because C++0x's result_of is decltype-based rather than using nested result<>/result_type types. The problem is that VC++ 2010's result_of appears to still rely on the TR1 protocol rather than use decltype. Presumably this will be addressed in a service-pack or in VC11, but in the meantime if you want to formally submit a bug report (because this is indeed a bug), submit it on MS Connect and post a link back here so we can vote it up.
-
Thursday, October 28, 2010 9:07 PM
If this is presenting a problem for you in real code, a workaround is to avoid use of std::result_of, which in practical terms means also avoiding use of std::bind -- use a lambda instead:
#include <functional> #include <iostream> #include <string> int main() { int value = 42; auto myBoundLambda = [&](const std::string& header) -> void { std::cout << header << value << std::endl; }; std::function<void()> myFunction = [=]() { myBoundLambda("Answer to life is: "); }; myFunction(); return 0; }
-
Tuesday, November 02, 2010 4:20 PM
Thank you for the help. Here is a link to the feedback posted to MS Connect:
https://connect.microsoft.com/VisualStudio/feedback/details/618807
- Marked As Answer by Jesse JiangMicrosoft Contingent Staff, Moderator Thursday, November 04, 2010 3:16 AM

