vil*_*apx 2 c++ boost boost-asio c++11
我正在尝试使用Boost.Asio的async_read_until()自由函数,但是我无法将成员函数指定为回调函数std::bind().我可以毫不费力地将这个习惯用于其他Boost.Asio函数,所以我很困惑为什么它不起作用async_read_until().
这是我的例子:
#include <functional>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
class Test
{
public:
boost::asio::streambuf buf;
boost::asio::ip::tcp::socket sock;
Test(boost::asio::io_service& io) :
sock(io)
{
}
void readComplete(const boost::system::error_code& error,
std::size_t bytesTransferred)
{
}
void invoke()
{
boost::asio::async_read_until(sock,
buf,
'\n',
std::bind(&Test::readComplete,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
};
int main(int argc, char** argv)
{
boost::asio::io_service io;
Test t(io);
t.invoke();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,编译失败并出现以下错误(以及其他template-compiler-error-barf):
In file included from /home/me/eclipse-workspace/practice/main.cpp:4:
In file included from /usr/include/boost/asio.hpp:91:
In file included from /usr/include/boost/asio/read_until.hpp:921:
/usr/include/boost/asio/impl/read_until.hpp:707:3: error: static_assert failed "ReadHandler type requirements not met"
BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/boost/asio/detail/handler_type_requirements.hpp:153:3: note: expanded from macro 'BOOST_ASIO_READ_HANDLER_CHECK'
BOOST_ASIO_HANDLER_TYPE_REQUIREMENTS_ASSERT( \
^
/usr/include/boost/asio/detail/handler_type_requirements.hpp:105:6: note: expanded from macro 'BOOST_ASIO_HANDLER_TYPE_REQUIREMENTS_ASSERT'
static_assert(expr, msg);
^ ~~~~
/home/me/eclipse-workspace/practice/main.cpp:24:22: note: in instantiation of function template specialization 'boost::asio::async_read_until<boost::asio::basic_stream_socket<boost::asio::ip::tcp,
boost::asio::stream_socket_service<boost::asio::ip::tcp> >, std::allocator<char>, std::_Bind<std::_Mem_fn<void (Test::*)(const boost::system::error_code &, unsigned long)>
(Test *, boost::arg<1> (*)(), boost::arg<2> (*)())> >' requested here
boost::asio::async_read_until(sock,
^
Run Code Online (Sandbox Code Playgroud)
所以它告诉我我的功能签名是错误的,它似乎并不是.
我不明白为什么它不工作,而我更是这样不明白的是,如果我只是换出std::bind()的boost::bind(),它建立和运作完全正常.有人可以帮我理解这个吗?
仅供我使用Boost 1.58.0(来自Ubuntu 16.04 Universe repos)并使用C++ 11进行++ 3.8.
不幸的是,你正在混合和匹配binds:
std::bind(&Test::readComplete,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
Run Code Online (Sandbox Code Playgroud)
该boost::asio::placeholders只工作boost::bind,不与std::bind.
从文档中,您需要:
std::bind(&Test::readComplete,
this,
std::placeholders::_1,
std::placeholders::_2));
Run Code Online (Sandbox Code Playgroud)
要么
boost::bind(&Test::readComplete,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
Run Code Online (Sandbox Code Playgroud)
我不明白为什么boost不只是说它的占位符是std占位符:
namespace std {
template <int I>
struct is_placeholder<boost::arg<I>>
: integral_constant<int, I>
{ };
}
Run Code Online (Sandbox Code Playgroud)
我认为有充分的理由吗?
| 归档时间: |
|
| 查看次数: |
252 次 |
| 最近记录: |