I get a compile error using this:
std::vector<std::function<int(int)>> functions; std::function<int(int, int)> foo = [](int a, int b){ return a + b; }; std::function<int(int)> bar = std::bind(foo, 2); functions.push_back(bar);
The error is:
/usr/include/c++/4.6/functional:1764:40: error: no match for call to '(std::_Bind(int)>) (int)'
Can someone tell me how to convert a std::bind
into a std::function
?
std::bind is for partial function application. That is, suppose you have a function object f which takes 3 arguments: f(a,b,c); You want a new function object which only takes two arguments, defined as: g(a,b) := f(a, 4, b);
std::bind allows you to create a std::function object that acts as a wrapper for the target function (or Callable object). std::bind also allows you to keep specific arguments at fixed values while leaving other arguments variable.
As far as I can tell std::bind just concatenates stuff together into an object, so binding anything to a member function will result in an object that is at least three pointers in size. Assigning this object to a std::function will result in dynamic memory allocation.
boost::bind is a generalization of the standard functions std::bind1st and std::bind2nd. It supports arbitrary function objects, functions, function pointers, and member function pointers, and is able to bind any argument to a specific value or route input arguments into arbitrary positions.
std::function<int(int)> bar = std::bind(foo, 2, std::placeholders::_1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With