在this example ,如果我用绑定(bind)更改此调用:
boost::asio::async_connect(
socket_,
results.begin(),
results.end(),
std::bind(
&session::on_connect,
shared_from_this(),
std::placeholders::_1));
对此:
auto self = shared_from_this();
boost::asio::async_connect(
socket_,
results.begin(),
results.end(),
[self](boost::system::error_code ec) {
self->on_connect(ec);
});
我得到一个断言错误:
boost/boost/asio/impl/connect.hpp:761: error: static_assert failed "IteratorConnectHandler type requirements not met"
BOOST_ASIO_ITERATOR_CONNECT_HANDLER_CHECK(
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
那里有一条评论:
// If you get an error on the following line it means that your handler does
// not meet the documented type requirements for a IteratorConnectHandler.
我个人don't prefer bind ,并想将其更改为 lambda。我做错了吗,或者这是 boost::beast 中的一个小错误?
顺便说一下,为 on_resolve
更改为 lambda 工作正常。
最佳答案
根据引用文献 async_connect,您的 lambda 参数数量与处理程序签名不匹配处理程序采用 error_code
和 connected endpoint - 它在您的案例中丢失。
修复:
auto self = shared_from_this();
boost::asio::async_connect(
socket_,
results.begin(),
results.end(),
[self](boost::system::error_code ec, boost::asio::ip::tcp::resolver::iterator) {
^^^
self->on_connect(ec);
});
关于c++ - 将 lambda 与 Boost Beast 一起使用而不是绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55540700/