简体   繁体   English

C ++ 11 std :: bind和boost :: bind之间的区别

[英]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? 或者我可以安全地在我的代码中用std::bind替换每次出现的boost::bind ,从而消除对Boost的依赖吗?

  • boost::bind has overloaded relational operators , std::bind does not. boost::bind 有重载的关系运算符std::bind没有。

  • boost::bind supports non-default calling conventions , std::bind is not guaranteed to (standard library implementations may offer this as an extension). boost::bind 支持非默认调用约定 ,不保证std::bind (标准库实现可以将此作为扩展提供)。

  • boost::bind provides a direct mechanism to allow one to prevent eager evaluation of nested bind expressions ( boost::protect ), std::bind does not. boost::bind提供了一种直接机制,允许一个人阻止对嵌套绑定表达式( boost::protect )的急切评估,而std::bind则没有。 (That said, one can use boost::protect with std::bind if they want, or trivially reimplement it on their own.) (也就是说,如果他们想要,可以使用boost::protectstd::bind ,或者自己重新实现它。)

  • 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. std::bind提供了一种直接机制,允许用户将任何用户定义的std::is_bind_expression函数视为嵌套的绑定表达式,以强制进行评估( std::is_bind_expression :[func.bind.isbind] / 1,[func.bind.bind] ] / 10), boost::bind没有。

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. boost::bind似乎在某些情况下处理重载的函数名称,而std::bind不以相同的方式处理它们。 See c++11 faq 请参阅c ++ 11常见问题解答

(using gcc 4.7.2, boost lib version 1_54) (使用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替换所有boost::bind std::bind ,那么您的构建可能会中断。

  • 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). std::bind可以无缝绑定到c ++ 11 lambda类型,而boost::bind似乎需要用户输入(除非定义了return_type)。 See boost doc 请参阅boost doc

(using gcc 4.7.2, boost lib version 1_54) (使用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. 所以,如果你只是用boost::bind替换所有std::bind boost::bind ,你的构建也可能会破坏。

Besides the listed above, boost::bind has an important extension point: get_pointer() function that allows integrating boost::bind with any smart pointer, eg. 除了上面列出的,boost :: bind还有一个重要的扩展点:get_pointer()函数,它允许将boost :: bind与任何智能指针集成,例如。 ATL::CComPtr etc. http://www.boost.org/doc/libs/1_49_0/libs/bind/mem_fn.html#get_pointer ATL :: CComPtr等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 因此,使用boost :: bind,您还可以绑定weak_ptr: http//lists.boost.org/Archives/boost/2012/01/189529.php

I don't have the full answer but std::bind will use variadic templates rather than parameter lists. 我没有完整的答案,但std::bind将使用可变参数模板而不是参数列表。

The placeholders are in std::placeholders as in std::placeholders::_1 rather than the global namespace. 占位符位于std::placeholdersstd::placeholders::_1而不是全局命名空间。

I alias the namespace to stdph with 我将命名空间别名为stdph with

namespace stdph=std::placeholders;

Apart from that I have had no problems updating to C++11 除此之外,我在更新到C ++ 11时没有遇到任何问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM