Effective Modern C++:bind and lambda

Function binding

std::bind can create a function which is binded to another one:

void originalFunc(std::string a, std::string b, std::string c)
    std::cout << a << " " << b << " " << c << std::endl;
}//original function, to be binded

void main()
  	//Newly binded function, created by bind function
  	auto newlyBindedFunc = bind(originalFunc, placeholders::_3, "Fixed", placeholders::_1);
    //Call the newly binded function
    newlyBindedFunc("New 1st",    "New 2nd",   "New 3rd");

We can see that calling

newlyBindedFunc(“New 1st”, “New 2nd”, “New 3rd”);

is exactly the same as calling

originalFunc(“New 3rd”, “Fixed”, “New 1st”);

So the bind function is just remapping the input arguments:


Just as the above diagram shows, using the bind in this case,

the 3rd(std::placeholders::_3) argument of newlyBindedFunc is mapped to the 1st input argument of originalFunc: string a.

the 1st(std::placeholders::1) argument of newlyBindedFunc is mapped to the 3rd input argument of originalFunc: string c.

the 2nd argument of originalFunc is already defined by std::bind as “fixed”.

Move things into Closure

Sometimes copy a object into a closure is very expensive(STL containers), we prefer moving them instead of copying them, but C++ 11 doesn’t support it.

Fortunately, we can use bind as a workaround:

	vector<int> originalData{ 1,2,3,4,5,6,7 };

	auto func = bind(
      [](vector<int>& data)
		  for (int i = 0; i < data.size(); i++)
			 cout << data[i] << " ";


The code is straightforward, we use std::bind to create a new function func, which is binded to a lambda. The lambda has one input and it’s defined by std::bind to be move(originalData).

So actually the input argument originalData is passed by bind function, when you are really calling func(), you don’t need to pass anything.

Wangxin -->


I am algorithm engineer focused in computer vision, I know it will be more elegant to shut up and show my code, but I simply can't stop myself learning and explaining new things ...