Search code examples
c++c++11boost-asioshared-ptrboost-bind

Compilation failure when using std::shared_ptr instead of boost::shared_ptr


The code below successfully sends an async message to the given endpoint.

// message is a boost::shared_ptr<std::string>

// open a UDP socket
boost::asio::ip::udp::socket socket(ioService);
socket.open(boost::asio::ip::udp::v4());

// create the remote endpoint
boost::asio::ip::udp::endpoint remoteEndpoint(boost::asio::ip::address_v4::from_string(address), port);

// asynchronously send a datagram to the remote endpoint
socket.async_send_to(boost::asio::buffer(*message),
                     remoteEndpoint,
                     boost::bind(&MyClass::handler,
                                 this,
                                 message,
                                 boost::asio::placeholders::error,
                                 boost::asio::placeholders::bytes_transferred));

socket.close();

However, if I change the type of message to a std::shared_ptr<std::string> rather than a boost::shared_ptr<std::string> then the call to async_send_to doesn't compile.

The error is:

boost/boost/bind/bind.hpp:457:9: No matching function for call to object of type 'boost::_mfi::mf3<void, MyClass, const boost::shared_ptr<std::__1::basic_string<char> > &, const boost::system::error_code &, unsigned long>'.

Can someone explain what is wrong? Is it possibly because I'm using boost::bind?


Solution

  • Looks like problem is, that you handler function receives boost::shared_ptr, not std::shared_ptr and boost::shared_ptr is not constructible from std::shared_ptr.