Difference between std::bind() and boost::bind() in C++
If you are searching for the difference between std:: bind() and boost:: bind() you have come at the right place. Here in this article, I will provide you with the differences so that you use them appropriately.
Before heading forward let me give you a brief intro to what a bind()
function does. A bind()
function takes a function as input and returns another function object in which one or more arguments are fixed or rearranged. E.g.
original_function( int a ,int b ,int c) { return a+b+c; } auto new_function = bind(original_function, 4, _1 , 6 ); // _1 is a placeholder at second place here which means the second // argument requires a value while the others have been fixed. new_function(5); // this will call the original_function but with only one argument // whose value goes to int b
To use std::bind()
you need to add a header file #include<functional>
and to use boost::bind()
add #include<boost/bind.hpp>
.
Now let’s see the difference.
std::bind() vs boost::bind()
- One of the primary differences between these two is that while using
std::bind()
the placeholders( _1, _2,…) should be ported by:
using namespace placeholders;
while no need for porting in boost:: bind() as it is already there in the global namespace, and if you port the above it will give an ambiguity error.
- The next difference is that
boost:: bind()
supports operators overloading whilestd:: bind()
does not. Bind overload produces
1. the logical not-operator !
2. the relational and logical operators ==, !=, <, <=, >, >=, &&, ||.
sort(v.begin(),v.end(), bind(function_name, _1) < bind(function_name, _2)); //compare
if(!bind(function_name, _1)){...} //negate the result
and all others in boost:: bind()
.
- Next,
boost::bind()
provides a direct way to allow one to prevent eager evaluation (a technique used where operations are performed right away, rather than postponing an operation until its result is required is called eager evaluation. Eager evaluation is undesired where we might end up constructing a value which is not utilized) of nested bind expressions using boost:: protect()), std:: bind() does not.
bind(function1 , bind(function2 , _1 , _2)); //nested bind
Sometimes if we don’t require to evaluate the subsequent subexpressions to the first, rather just pass the arguments to the next function object we use:
boost:: protect( bind(function1, ....) );
- The next difference is in the way they handle function overloading. Here is an example:
#include <iostream> #include <functional> #include <boost/bind.hpp> int result(int a,int b,int c){ return a+b-c ; } int result(int a){ return a+2; } int main() { auto g= boost:: bind(result,4,5,_1); auto k= boost:: bind(result,_1); // auto h= std:: bind(result, 4,5, std::placeholders::_1); // gives error auto h= std::bind(static_cast<int(*)(int)>(result), std::placeholders::_1); // correct way return 0; }
- Another difference comes up when dealing with lambda types (functions without names). Here is an example:
#include <iostream> #include <functional> #include <boost/bind.hpp> int main() { auto store = [](int i) // lambda function { return i;}; auto stdcorrect = std::bind(store, std::placeholders::_1); // auto boostwrong = boost::bind(store, _1); // wrong auto boostcorrect = boost::bind<int>(store, _1); // correct auto boostcorrect1 = boost::bind(boost::type<int>(), store, _1); // alternate correct return 0; }
Thus we see the differences in std:: bind() and boost:: bind() although they are doing the same job. And also, interchanging them in a code is not a good idea.
That’s it for this post hope the difference is clear now.
Leave a Reply