两者之间有什么区别吗?还是我可以安全地替换代码中所有出现的boost::bindby std::bind,从而消除对Boost的依赖?
Answers:
boost::bind 有关系运算符重载,std::bind没有。
boost::bind 支持非默认调用约定,std::bind不保证(标准库的实现可以提供此作为一个扩展)。
boost::bind提供了一种直接的机制,可以防止对嵌套绑定表达式(boost::protect)进行热切评估,std::bind而不能。(也就是说,可以使用boost::protect与std::bind如果他们愿意,或平凡重新实现它自己。)
std::bind提供了一种直接的机制,允许人们将任何用户定义的函子视为嵌套的绑定表达式,以强制进行急切的求值(std::is_bind_expression:[func.bind.isbind] / 1,[func.bind.bind] / 10)boost::bind没有。
除了在其他答案中引用的几个差异之外,这还有两个其他差异:
boost::bind似乎在某些情况下处理重载的函数名,而std::bind不会以相同的方式处理它们。参见 C ++ 11常见问题解答(使用gcc 4.7.2,提升lib版本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 lambda类型,而boost::bind从boost 1.54开始,似乎需要用户输入(除非定义了return_type)。参见增强文档(使用gcc 4.7.2,提升lib版本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,则构建也可能会中断。
除了上面列出的内容外,boost :: bind还有一个重要的扩展点:get_pointer()函数,可以将boost :: bind与任何智能指针集成在一起。ATL :: CComPtr等 。http://www.boost.org/doc/libs/1_49_0/libs/bind/mem_fn.html#get_pointer
结果,使用boost :: bind您还可以绑定weak_ptr:http : //lists.boost.org/Archives/boost/2012/01/189529.php
INVOKE标准中的功能可与任何支持的智能指针配合使用operator*
我没有完整的答案,但std::bind将使用可变参数模板而不是参数列表。
占位符std::placeholders位于std::placeholders::_1而不是全局命名空间中。
我将命名空间命名为stdph
namespace stdph=std::placeholders;
除此之外,我没有任何问题更新到C ++ 11
boost::bind直接或间接使用。
std::bind已经被大量复制boost::bind。