未满足Boost::ASIO读取处理程序类型要求
我正在尝试将Read Header处理程序实现为处理传入数据的成员函数。但是,在编译过程中,我被告知未满足类型要求。
我已经检查了Boost文档,函数签名似乎没有问题。我看不出有什么不同。但是,Boost不接受该处理程序为有效处理程序。
.cpp文件:
void tcpclient::read_data() {
char buffer_[1];
boost::asio::async_read(_socket, boost::asio::buffer(buffer_, HEADER_LEN),
std::bind(&tcpclient::handle_read_header, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void tcpclient::handle_read_header(const boost::system::error_code &error, std::size_t bytes_transferred) {
if (!error) {
logger::log_info("Read " + std::to_string(bytes_transferred) + " bytes.");
} else {
logger::log_error("Failed to read header");
_socket.close();
}
}
.h文件:
void handle_read_header(const boost::system::error_code &error, std::size_t bytes_transferred);
void read_data();
boost::asio::ip::tcp::socket _socket;
我希望代码能够很好地接受处理程序签名,但结果却提示我:
/usr/include/boost/asio/impl/read.hpp: In instantiation of ‘typename boost::asio::async_result<typename std::decay<WriteHandler>::type, void(boost::system::error_code, long unsigned int)>::return_type boost::asio::async_read(AsyncReadStream&, const MutableBufferSequence&, ReadHandler&&, typename std::enable_if<boost::asio::is_mutable_buffer_sequence<MutableBufferSequence>::value>::type*) [with AsyncReadStream = boost::asio::basic_stream_socket<boost::asio::ip::tcp>; MutableBufferSequence = boost::asio::mutable_buffers_1; ReadHandler = std::_Bind<void (tcpclient::*(tcpclient*, boost::arg<1> (*)(), boost::arg<2> (*)()))(const boost::system::error_code&, long unsigned int)>; typename boost::asio::async_result<typename std::decay<WriteHandler>::type, void(boost::system::error_code, long unsigned int)>::return_type = void; typename std::enable_if<boost::asio::is_mutable_buffer_sequence<MutableBufferSequence>::value>::type = void]’:
/home/void/Documents/Development/SocketTest/SocketTest/networking/tcpclient.cpp:55:84: required from here
/usr/include/boost/asio/impl/read.hpp:446:3: error: static assertion failed: ReadHandler type requirements not met
BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/boost/asio/impl/write.hpp:430:3: error: no match for call to ‘(std::_Bind<void (tcpclient::*(tcpclient*, boost::arg<1> (*)(), boost::arg<2> (*)()))(const boost::system::error_code&, long unsigned int)>) (const boost::system::error_code&, const long unsigned int&)’
BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
解决方案
使用boost::bind
与boost::asio::placeholders::..
boost::bind(&tcpclient::handle_read_header, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
或std::placeholders::_1/_2
std::bind
:
std::bind(&tcpclient::handle_read_header, this,
std::placeholders::_1,
std::placeholders::_2));
相关文章