Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between C++11 std::bind and boost::bind

Is there any difference between the two? Or am I safe to replace every occurrence of boost::bind by std::bind in my code and thereby remove the dependence on Boost?

like image 935
Haatschii Avatar asked May 11 '12 16:05

Haatschii


People also ask

What is boost :: bind?

boost::bind is a generalization of the standard functions std::bind1st and std::bind2nd. It supports arbitrary function objects, functions, function pointers, and member function pointers, and is able to bind any argument to a specific value or route input arguments into arbitrary positions.

What does std :: bind do in C++?

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.

What is the return type of std :: bind?

std::bind. Returns a function object based on fn , but with its arguments bound to args . Each argument may either be bound to a value or be a placeholder: - If bound to a value, calling the returned function object will always use that value as argument.

Is std :: bind deprecated?

Yes: std::bind should be replaced by lambda For almost all cases, std::bind should be replaced by a lambda expression. It's idiomatic, and results in better code. There is almost no reason post C++11 to use std::bind .


2 Answers

  • boost::bind has overloaded relational operators, std::bind does not.

  • boost::bind supports non-default calling conventions, std::bind is not guaranteed to (standard library implementations may offer this as an extension).

  • boost::bind provides a direct mechanism to allow one to prevent eager evaluation of nested bind expressions (boost::protect), std::bind does not. (That said, one can use boost::protect with std::bind if they want, or trivially reimplement it on their own.)

  • std::bind provides a direct mechanism to allow one to treat any user defined functor as a nested bind expression in order to force eager evaluation (std::is_bind_expression: [func.bind.isbind]/1, [func.bind.bind]/10), boost::bind does not.

like image 181
ildjarn Avatar answered Oct 12 '22 23:10

ildjarn


Besides the several differences cited on the other answers, here are two other differences:

  • boost::bind seems to deal with overloaded function names in some situations, whereas std::bind does not deal with them in the same way. See c++11 faq

(using gcc 4.7.2, boost lib version 1_54)

void foo(){} void foo(int i){}  auto badstd1 = std::bind(foo);   //compile error: no matching function for call to bind(<unresolved overloaded function type>) auto badstd2 = std::bind(foo, 1);  //compile error: no matching function for call to bind(<unresolved overloaded function type>) auto std1 = std::bind(static_cast<void(*)()>(foo)); //compiles ok auto std2 = std::bind(static_cast<void(*)(int)>(foo), 1); //compiles ok auto boost1 = boost::bind(foo, 1); //compiles ok auto boost2 = boost::bind(foo); //compiles ok 

So if you simply replaced all boost::bind with std::bind, your build could break.

  • std::bind can seamlessly bind to c++11 lambda types, whereas boost::bind as of boost 1.54 seems to require input from the user (unless return_type is defined). See boost doc

(using gcc 4.7.2, boost lib version 1_54)

auto fun = [](int i) { return i;}; auto stdbound = std::bind(fun, std::placeholders::_1); stdbound(1);  auto boostboundNaive = boost::bind(fun, _1);  //compile error. // error: no type named ‘result_type’ ... auto boostbound1 = boost::bind<int>(fun, _1); //ok boostbound1(1); auto boostbound2 = boost::bind(boost::type<int>(), fun, _1); //ok boostbound2(1); 

So, if you simply replaced all std::bind with boost::bind, your build could also break.

like image 38
orm Avatar answered Oct 13 '22 00:10

orm