Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::bind to std::function?

Tags:

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?

like image 285
Philipp H. Avatar asked Jul 07 '12 06:07

Philipp H.


People also ask

What is std :: bind () in CPP?

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);

How does STD bind work?

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.

Does STD bind allocate?

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.

What is boost bind?

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.


1 Answers

std::function<int(int)> bar = std::bind(foo, 2, std::placeholders::_1); 
like image 55
inkooboo Avatar answered Nov 02 '22 13:11

inkooboo