Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 476
BD at Rivenhill Avatar asked Jul 01 '13 19:07

BD at Rivenhill


2 Answers

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

like image 172
pal Avatar answered Oct 15 '22 08:10

pal


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.

like image 4
Praetorian Avatar answered Oct 15 '22 08:10

Praetorian