使用std :: shared_ptr而不是boost :: shared_ptr时编译失败

时间:2015-02-04 14:36:51

标签: c++ c++11 boost-asio shared-ptr boost-bind

以下代码成功向指定端点发送异步消息。

// 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吗?

1 个答案:

答案 0 :(得分:5)

看起来问题是,handler函数收到boost::shared_ptr,而不是std::shared_ptrboost::shared_ptr无法从std::shared_ptr构建。

相关问题