有没有两者之间有什么区别? 还是我的安全,以取代所有出现boost::bind
通过std::bind
在我的代码,从而消除对加速的依赖?
Answer 1:
boost::bind
已重载关系运算符 ,std::bind
没有。boost::bind
支持非默认调用约定 ,std::bind
不保证(标准库的实现可以提供这作为一个扩展)。boost::bind
提供了一个直接的机制,允许一个防止嵌套绑定表达式的渴望评估(boost::protect
)std::bind
没有。 (这就是说,一个可以使用boost::protect
用std::bind
,如果他们想,还是平凡重新实现它自己。)std::bind
提供了一种直接的机制,以允许一个治疗任何用户定义函子作为嵌套bind表达式,以迫使及早求值(std::is_bind_expression
:[func.bind.isbind] / 1 [func.bind.bind ] / 10),boost::bind
没有。
Answer 2:
除了在其他的答案中引用的一些差异,这里有另外两个区别:
-
boost::bind
看来对付在某些情况下重载函数名,而std::bind
不与他们以同样的方式处理。 见C ++ 11 FAQ
(使用gcc 4.7.2,提高库版本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
所以,如果你简单地更换所有boost::bind
使用std::bind
,您的构建可能会断裂。
-
std::bind
可以无缝地结合到C ++ 11种拉姆达类型,而boost::bind
作为升压1.54似乎需要来自用户的输入(除非return_type被定义)。 见升压DOC
(使用gcc 4.7.2,提高库版本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);
所以,如果你简单地更换所有std::bind
使用boost::bind
,您的构建也突破。
Answer 3:
除了上面列出的boost ::绑定有一个重要的扩展点:get_pointer(),允许使用任何智能指针集成的boost ::绑定功能,例如。 ATL ::但是CComPtr等http://www.boost.org/doc/libs/1_49_0/libs/bind/mem_fn.html#get_pointer
其结果是,用升压::绑定你也可以绑定的weak_ptr: http://lists.boost.org/Archives/boost/2012/01/189529.php
Answer 4:
我没有完整的答案,但std::bind
将使用可变参数模板,而不是参数列表。
占位符是std::placeholders
为std::placeholders::_1
,而不是全局命名空间。
我别名命名空间来stdph
namespace stdph=std::placeholders;
从我除了有没有问题升级到C ++ 11