How to use lambda functions with boost::bind/std::bind in VC++ 2010?

18,188

Solution 1

You need to manually specify the return type:

boost::bind<void>(f)();
boost::bind<int>(f2, 13)();

You can also write yourself a template-function to deduce the return type automagically using Boost.FunctionTypes to inspect your lambda's operator(), if you don't like to explicitly tell bind.

Solution 2

std::function<void ()> f1 = [](){ std::cout<<"f1()"<<std::endl; };
std::function<void (int)> f2 = [](int x){ std::cout<<"f2() x="<<x<<std::endl; };
boost::function<void ()> f3 = [](){ std::cout<<"f3()"<<std::endl; };
boost::function<void (int)> f4 = [](int x){ std::cout<<"f4() x="<<x<<std::endl; };

//do you still wanna bind?
std::bind(f1)(); //ok
std::bind(f2, 13)(); //ok
std::bind(f3)(); //ok
std::bind(f4, 13)(); //ok

//do you still wanna bind?
boost::bind(f1)(); //ok
boost::bind(f2, 13)(); //ok
boost::bind(f3)(); //ok
boost::bind(f4, 13)(); //ok

Solution 3

I think you might be interested in this MSDN forum post. Sounds like the poster had the same problem as yours, and lodged a bug with MS Connect.

Share:
18,188

Related videos on Youtube

Timo
Author by

Timo

Updated on January 23, 2020

Comments

  • Timo
    Timo over 4 years

    I have some lambda functions which I want to bind using either boost::bind or std::bind. (Don't care which one, as long as it works.) Unfortunately both of them give me different compiler erros:

    auto f = [](){ cout<<"f()"<<endl; };
    auto f2 = [](int x){ cout<<"f2() x="<<x<<endl; };
    
    std::bind(f)(); //ok
    std::bind(f2, 13)(); //error C2903: 'result' : symbol is neither a class template nor a function template
    
    boost::bind(f)(); //error C2039: 'result_type' : is not a member of '`anonymous-namespace'::<lambda0>'
    boost::bind(f2, 13)(); //error C2039: 'result_type' : is not a member of '`anonymous-namespace'::<lambda1>'
    

    So, what is the simplest workaround for this?

    • Jagannath
      Jagannath over 13 years
      Why do you want to use bind ? Can't you just call f() or f2(1) ?
    • Timo
      Timo over 13 years
      @Jagannath: This was of course just a simple example, but in reality i wanna store the bind result into a function object.
    • Alexandre C.
      Alexandre C. over 13 years
      Why do you want to use bind ? I thought C++0x provided closures ?
  • Hans-Kristian Norum Eidesen
    Hans-Kristian Norum Eidesen over 13 years
    Only if function introduces runtime-indirection and bind is faster ... Is it?
  • ds27680
    ds27680 over 13 years
    I doubt calling the functor resulting from bind would ever be faster since the bind result adds in most cases yet another level of indirection. bind will probably be required in situations where some parameters need to be bound. In the above example std::bind(f2, 13)(); binds the first parameter to the constant value 13. The result of the bind is a "functor" that takes no parameters and returns void.
  • ltjax
    ltjax over 13 years
    Calling the result of the bind could be slightly faster, since there's no call through a virtual function as with function.
  • ds27680
    ds27680 over 13 years
    @ltjax If you try it and debug it you will see the same thing happens in both cases. The difference is that with bind the depth of the callstack until the actual lamda is called is greater
  • ds27680
    ds27680 over 13 years
    @ltjax My previous comment was for std::bind. With boost::bind and boost::function what happens when one calls the bound functor or the boost::function is indeed different and you might be right it could be faster to call on the bind result
  • ds27680
    ds27680 over 13 years
    @ltjax Although in my tests calling the boost::function directly is marginally faster than calling on the bind result (over 10000 calls).