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

Is there a difference between the two? Or can I replace every boost::bind event with std::bind in my code and thereby eliminate the Boost dependency?

+61
c ++ boost c ++ 11 boost-bind stdbind
May 11 '12 at 16:56
source share
4 answers
  • boost::bind has overloaded relational operators , std::bind does not.

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

  • boost::bind provides a direct mechanism to prevent impatient evaluation of nested binding expressions ( boost::protect ), std::bind not. (However, you can use boost::protect with std::bind if they want, or trivially override it yourself.)

  • std::bind provides a direct mechanism to handle any user-defined functor as an expression of nested binding to make an impatient estimate ( std::is_bind_expression : [func.bind.isbind] / 1, [func.bind.bind] / 10), boost::bind no.

+83
May 11 '12 at 17:13
source share

Besides the few differences mentioned in the other answers, there are two other differences:

  • boost::bind seems to deal with overloaded function names in some situations, while std::bind does not work with them 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 just replaced all boost::bind with std::bind , your assembly might break.

  • std::bind can easily bind to C ++ 11 lambda types, while boost::bind with a boost of 1.54 seems to require user input (unless return_type is specified). See more details.

(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 just replaced all std::bind with boost::bind , your assembly may also break.

+22
Aug 07 '13 at 0:36
source share

In addition to the above, boost :: bind has an important extension point: get_pointer (), which allows you to integrate boost :: bind with any smart pointer, for example. 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 weak_ptr: http://lists.boost.org/Archives/boost/2012/01/189529.php

+16
May 13 '12 at 9:54 a.m.
source share

I do not have a complete answer, but std::bind will use variation patterns, not parameter lists.

Alternates are in std::placeholders as in std::placeholders::_1 , and not in the global namespace.

I, like the namespace, in stdph with

 namespace stdph=std::placeholders; 

Also, I had no problems upgrading to C ++ 11

+8
May 11 '12 at 16:58
source share



All Articles