In this article we will see how to use std::bind and when to use.

std::bind

std::bind is a Standard Function Objects that acts as a Functional Adaptor i.e. it takes a function as input and returns a new function Object as an output with with one or more of the arguments of passed function bound or rearranged.

Suppose We have a function to add two numbers i.e.

std::bind takes a function as its first parameter and then that function’s argument as its parameter.

Here add_func is a function object i.e. equivalent to add().
std::bind took the first parameter a function i.e. &add and then its arguments as _1 & _2 as his own arguments.

So, whenever we call this new function object i.e.

It will internally call the add() function and pass the first parameter at the place of _1 and second at the place of _2.

So, add_func(4,5) is equivalent to add(4,5).

Now suppose we want to use this add function in a special scenarion where we should fix the first argument to 12 always and let the second argument passed by user i.e.

We can do this using std::bind,

So, when we call new_add_func(5) it will internally call the add() function with first parameter will be  12 always and second parameter will 5 i.e. passed as argument.

We can also rearrange arguments using std::bind() i.e. _1 and _2 etc decides the place of argument to be passed i.e.

When we call mod_add_func(12,15) it is equivalent to calling add(15, 12).

_1 represents the first passed argument and _2 second passed argument. Now while constructing new function object through std::bind we changed the order of arguments by passing _2 first and _1 second in the underlined function.

Use of std::bind in STL algorithms

As std::bind acts as a functional adaptor and gives the a new function objects, hence it is very usefull with many STL algorithms.

For example,
We have  a list of numbers and we want to count the numbers which are multiple of 5. To achive this we have an existing function i.e.

First basic approach to this problem is lets iterate through all elements using for loop, check condition and increment counter i.e.

 

But this all is done in std::count_if STL algorithm i.e. std::count_if

count_if (InputIterator firstValue, InputIterator lastValue, UnaryPredicate predFunctionObject);

std::count_if Returns the number of elements in the range [firstValue,lastValue) for which predFunctionObject is true.

Now to use this std::count_if in our scenario we need to convert divisible() into a Unary predicate i.e. Fixing second value in function divisible () to 5 always. Let’s do this with std::bind,

Here, std::bind(&divisible, _1, 5) Will return a new Function Object that takes only One Argument and checks if it is divissble by 5 or not.

What std::bind returns ?

std::bind returns a function object. In above examples we have either save this new function object in auto variable or used it directly.
But we can also store them using std::function Function object i.e.

To checkout more on std::function, checkout our next Article.

 

Complete executable code is as follows,