Pass lambda with the parameter

advertisements

I would like to pass a lambda to a funciton.

This

boost::function<void()> fncPtr(boost::bind<void>([](){/* something */}));

works, but if the lambda had a parameter, I don't know how to do it properly:

boost::function<void(bool)>
fncPtr(boost::bind<void,bool>([](bool){/* something */}, _1));

does not work.

Where I am wrong? How to pass lambda with argument(s)?

I would like to do this in a member function. So in "global scope"(is it the name?) this method above works fine.


You have a problem in the compiler probably. I have an error in Visual Studio 2010 too. You can help compiler to convert a lambda to a function using helper function:

template <typename T>
void closureToFunction(bool x){ T f; return f(x); }

int main()
{
    auto exp = [](bool x){/* something */};
    boost::function<void(bool)> fncPtr(
      boost::bind( closureToFunction<decltype(exp)>, _1) );
    return 0;
}