我应该看到std :: bind和boost :: bind之间存在显着差异吗?

时间:2013-07-01 19:33:55

标签: c++ c++11 boost boost-asio stdbind

我正在探索对g ++ - 4.7(Ubuntu / Linaro 4.7.3-2ubuntu~12.04,具体而言)对C ++ 11的支持,我似乎找到了差异。

特别是,如果我注释掉#include <boost/bind.hpp>并在Boost ASIO异步客户端示例(取自http://www.boost.org/doc/libs/1_45_0/doc/html/boost_asio/example/http/client/async_client.cpp)中系统地将boost::bind的出现替换为std::bind,则该程序不再编译。

对此有何解释?

2 个答案:

答案 0 :(得分:7)

#include <functional>
namespace boost {
    namespace asio {
        namespace stdplaceholders {
            static decltype ( :: std :: placeholders :: _1 ) & error = :: std :: placeholders :: _1;
            static decltype ( :: std :: placeholders :: _2 ) & bytes_transferred = :: std :: placeholders :: _2;
            static decltype ( :: std :: placeholders :: _2 ) & iterator = :: std :: placeholders :: _2;
            static decltype ( :: std :: placeholders :: _2 ) & signal_number = :: std :: placeholders :: _2;
        }
    }
}

并使用boost::asio::stdplaceholders::*代替boost::asio::placeholders::*

答案 1 :(得分:4)

boost::asio::placeholders似乎无法与std::bind一起使用。在您链接到的示例中,第一次调用boost::bind发生在以下代码中:

resolver_.async_resolve(query,
    boost::bind(&client::handle_resolve, this,
      boost::asio::placeholders::error,
      boost::asio::placeholders::iterator));

简单地将boost::bind替换为std::bind会导致一堆错误。要进行编译,您需要将boost::asio::placeholders替换为std::placeholders

resolver_.async_resolve(query,
    std::bind(&client::handle_resolve, this,
      std::placeholders::_1,
      std::placeholders::_2));

请注意,在进行这些更改之后,我还没有验证代码在功能上是否相同,只是它编译了。