Effective Modern C++:bind and lambda
Function binding
std::bind can create a function which is binded to another one:
void originalFunc(int a, double b, string c)
{
cout << a << " " << b << " " << c.c_str() << endl;
}//original function, to be binded
void main()
{
//Newly binded function, created by bind function
auto newlyBindedFunc = bind(originalFunc,
placeholders::_2,
10.5,
placeholders::_1);
newlyBindedFunc("Hello world",2);//call the new func
originalFunc(2,10.5,"Hello world");//the same result
}
We can see that calling
newlyBindedFunc(“Hello world”,2)
is exactly the same as calling
originalFunc(2,10.5,”Hello world”);
So the bind function is just remapping the input arguments:
Just as the above diagram shows, using the bind in this case,
the 1st argument of newlyBindedFunc is mapped to the 2nd(std::placeholders::_2) undefined input argument of originalFunc: in this case the 3rd input argument of originalFunc : string c.
the 2nd argument of newlyBindedFunc is mapped to the 1st(std::placeholders::_1) undefined input argument of originalFunc: in this case the 1st input argument of originalFunc : int a.
the 2nd argument(double b) of originalFunc is already defined by std::bind as 10.5
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] << " ";
}
},
move(originalData)
);
func();
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.