The bind template is an function template that creates and returns a function object that “wraps” a supplied function in the form of a function pointer. Its basic form is:

bind(function pointer, bound arguments)

For example, let sum_it be a function that takes 2 integer arguments and returns an int that is the sum of its arguments:

int multiply_it(int x, int y,int ) { int prod = x*y*z; cout << "x*y*z=" << prod << endl; return prod; } Let intx, inty,intz be three int variables. Then bind(multiply_it, intx, inty,intz)

creates and returns a function object whose function call operator takes no arguments and calls multiply_it with the values in the three integer variables, and returns an int. We can invoke the function call operator with a call with no additional arguments in the function call argument list, as in

int result = bind(multiply_it, intx, inty,intz)();

The final effect is as if multiply_it was called as

int result = multiply_it(intx,inty,intz);

Placeholders:

A placeholder is an object that specifies the number of parameter in the bound function that will be used to enter this parameter or simply we can say the final function arguments can be taken from a mixture of bound arguments and the arguments supplied in the call.

For example, the following would result in a call to multiply_it with the same input as the above examples.

bind(multiply_it, _2, inty, _1)(int3, int1).

The placeholders in the bound argument list are the special symbols _2 and _1. To make it possible to avoid name collisions, these are in the special namespace std::placeholders. The using directive above allows us to refer to them directly, instead of say std::placeholders::_1

Thus, in the above example, the placeholder _2 appears first in the bound argument list, which means it specifies the first argument to be given to multiply_it. Its number, 2, specifies the second argument in the call argument list. Thus the second call argument item will be the first function argument. Similarly, _1 in the third position means that this argument to multiply_it should be taken from the first of the supplied values in the call arguments. In this case, bind creates a function object that can be called with two integer values, the first of which is given to multiply_it as its third argument, the second of which is given to multiply_it as its first argument, and the second argument given to multiply_it is the bound value of inty.

Below is an example referenced from

C++11: std::function and std::bind

#include <functional> #include <iostream> using namespace std; using namespace std::placeholders; int add(int a, int b) { return a + b; } int main() { auto fn = bind(add, 6, _1); for (int i = 0; i < 5; i++) { cout << "6 + " << i << " = " << fn(i) << endl; } return 0; }

In std::bind : The first parameter is the pointer to the function multiply . The second parameter is the value passed as first parameter to the function multiply. The third parameter is called a “placeholder”. we can call fn just passing it one parameter.

In placeholder we can also change the order of argument.Below is an example

#include <functional> #include <string> #include <iostream> using namespace std; using namespace std::placeholders; void showdata(const string& x, const string& y, const string& z) { cout << x << "," << y << ", " << z << endl; } int main() { auto a = bind(showdata, _1, _2, _3); auto b = bind(showdata, _2, _1, _3); auto c = bind(showdata, "hi", _2, _1); x("sun", "mon", "tue"); y("sun", "mon", "tue"); z("sun", "tue"); return 0; } Output: sun, mon, tue mon, sun, tue hi; tue; sun

References: