6

我正在探索 g++-4.7(具体来说是 Ubuntu/Linaro 4.7.3-2ubuntu~12.04)上对 C++11 的支持,我似乎正在寻找差异。

特别是,如果我在Boost #include <boost/bind.hpp>ASIO异步客户端示例(取自http://www.boost.org/doc/libs/1_45_0/doc/html/boost_asio/example/http/client /async_client.cpp),程序不再编译。boost::bindstd::bind

对此有何解释?

4

2 回答 2

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::*

于 2013-07-01T22:15:48.743 回答
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::bindstd::bind会导致一堆错误。要使其编译,您需要替换boost::asio::placeholdersstd::placeholders.

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

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

于 2013-07-01T20:25:38.893 回答