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

后端 未结 3 2077
太阳男子
太阳男子 2021-02-07 09:25

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 differe

相关标签:
3条回答
  • 2021-02-07 09:29

    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.

    0 讨论(0)
  • 2021-02-07 09:38
    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
    
    0 讨论(0)
  • 2021-02-07 09:48

    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.

    0 讨论(0)
提交回复
热议问题