简体   繁体   中英

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?

看起来问题是,您的handler函数收到了boost::shared_ptr ,而不是std::shared_ptr并且boost::shared_ptr无法从std::shared_ptr构造。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM