Should I be seeing significant differences between std::bind and boost::bind?

不羁岁月 提交于 2019-12-07 03:56:54

问题


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.



来源:https://stackoverflow.com/questions/17412267/should-i-be-seeing-significant-differences-between-stdbind-and-boostbind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!