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?

- 1,182
- 2
- 17
- 34

- 9,021
- 10
- 58
- 95
-
I believe `std::bind` was pretty much copied from `boost::bind` when they came out with C++11, as with quite a few other things. – chris May 11 '12 at 16:56
-
10The question is about the "pretty much" part though. With some of the things that were lifted from Boost, minor changes were made. – jalf May 11 '12 at 16:58
4 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 useboost::protect
withstd::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.

- 62,044
- 9
- 127
- 211
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, whereasstd::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, whereasboost::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.

- 2,835
- 2
- 22
- 35
Besides the listed above, boost::bind has an important extension point: get_pointer() function that allows integrating boost::bind with any smart pointer, eg. ATL::CComPtr etc. http://www.boost.org/doc/libs/1_49_0/libs/bind/mem_fn.html#get_pointer
As a result, with boost::bind you can also bind a weak_ptr: http://lists.boost.org/Archives/boost/2012/01/189529.php

- 14,716
- 2
- 49
- 83
-
2The `INVOKE` functionality in the standard works with any smart pointer that support `operator*` – Jonathan Wakely Sep 16 '15 at 14:24
I don't have the full answer but std::bind
will use variadic templates rather than parameter lists.
The placeholders are in std::placeholders
as in std::placeholders::_1
rather than the global namespace.
I alias the namespace to stdph with
namespace stdph=std::placeholders;
Apart from that I have had no problems updating to C++11

- 15,686
- 6
- 47
- 62
-
When porting existing boost::bind code that use placeholders, adding "using namespace std::placeholders;" at the top of the file puts the placeholders into the global namespace. Very handy. – goertzenator Feb 07 '13 at 16:23
-
1the problem is, when porting you usually end up with boost bind still wrangling it;s way through somehow and you end up with the standard and boost placeholders. – 111111 Feb 07 '13 at 17:24
-
It depends on the project I guess. I mechanically removed all my boost function.hpp and bind.hpp includes from a decent size project with sed and the above namespace directive worked out fine. If you have boost bind in some header that you can't change, I see how things could get ugly. – goertzenator Feb 07 '13 at 20:03
-
2@goertzenator : I think the issue is more about use of any _other_ Boost libraries, as I'd wager at least a third of Boost uses `boost::bind` directly or indirectly. – ildjarn Sep 09 '15 at 18:53