Basic Things worth Knowing About Bind Adapter

Tips and Tricks

One of the new features that I really enjoy with C++11 is the bind adapter, because there are so many thing that you could do with it and it really makes your life simpler. For example, how many time we need to add a value to a collection. For example in our programming environment we have a library of C function that perform some mathematical operation and want to use it with stl algorithm for example.
Below I show some basic applications of everyday life programmer.

Before C++11 we had those 2 adapter bind1st and bind2nd, which take a binary function and treat it as unary function. Now these are deprecated and we have the bind adapter but still can achieve the same operation and more.

Initialize vector with list initializer (boost assign library)

std::vector<int> range1 = list_of(-1)(2)(5)(6)(-3);
std::vector<int> w_res; //store result

In the example below I subtract each element of the collection by some values. I use the pre-defined function object std::minus (binary function operator () (arg1,arg2)). First I want to perform the following operation: agr1 – arg2 (bind first argument arg1=constant). With the bind adapter we can define on-the-fly a function object with first argument binded. We can use it with the transform algorithm of the stl which take a unary function

Bind the first argument (bind1st )

Define a binary function and initialize it with bind adapter (same as bind1st which is now deprecated since C++11)

auto w_minusBind1st = std::bind(std::minus<int>(),1,_1);// apply function to data
std::transform( range1.begin(),range1.end(),std::back_inserter(w_res),

In the next example I want to perform the following (arg2-constant), mean that we are binding the second argument to a constant.

Bind the second argument (bind2nd )

Define a binary function and initialize it (same as bind2nd which is now deprecated since C++11)

auto w_minusBind2nd = std::bind(std::minus<int>(),_1,1);
// stl algorithm

In the next example I want to pass it as a function argument (very handful when prototyping, just want to try something and see what the result is).

Pass it as a function argument

Define a binary function that takes as argument a binary function and returns an integer. We use “function”, newly added to C++11.

typedef std::function<int(int,int)> BinaryFunc;

void myTestStdFunc( BinaryFunc aBfunc)
// let’s call this function
std::cout << “Calling Binary function and the result is: ” << aBfunc(1,4) << “\n”;

Initialize a binary function using the bind mechanism (on-the-fly). “Plus” is pre-defined binary function that has the same signature the “BinaryFunc” defined above and then pass as argument

auto w_plusbind2nd = std::bind( std::plus<int>(), _1,_2);

myTestStdFunc( w_plusbind2nd); // call function

Isn’t nice can make some construct on-the-fly and test it right away. It’s something that I do almost every day as a scientific programmer. Usually those functions are mathematical algorithm and we may want to validate their behavior or result.

Writing my own function object)

You can even use some function of your library. In our programming environment we have a library of function that performs math operation (signature like C-function). In many situations we want to test with some values, again bind can be used to fine tune your test. We have the following function prototype.

clients and partners