以下代码成功向指定端点发送异步消息。
// 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();
但是,如果我将message
的类型更改为std::shared_ptr<std::string>
而不是boost::shared_ptr<std::string>
,则对async_send_to
的调用不会编译。
错误是:
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>'.
有人可以解释什么是错的吗?可能是因为我使用了boost :: bind吗?
答案 0 :(得分:5)
看起来问题是,handler
函数收到boost::shared_ptr
,而不是std::shared_ptr
且boost::shared_ptr
无法从std::shared_ptr
构建。