Should I be seeing significant differences between

2019-04-29 11:14发布

问题:

I'm exploring the support for C++11 on the g++-4.7 (Ubuntu/Linaro 4.7.3-2ubuntu~12.04, to be specific) and I seem to be finding differences.

In particular, if I comment out #include <boost/bind.hpp> and systematically replace occurrences of boost::bind with std::bind in the Boost ASIO async client example (taken from http://www.boost.org/doc/libs/1_45_0/doc/html/boost_asio/example/http/client/async_client.cpp), the program no longer compiles.

Any explanation for this?

回答1:

#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;
        }
    }
}

and use boost::asio::stdplaceholders::* instead of boost::asio::placeholders::*



回答2:

It looks like boost::asio::placeholders cannot be used in conjunction with std::bind. In the example you've linked to, the first call to boost::bind occurs in the following code:

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

Simply replacing boost::bind with std::bind leads to a bunch of errors. To make it compile you need to replace boost::asio::placeholders with std::placeholders.

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

Note that I haven't verified that the code is functionally the same after making these changes, only that it compiles.