c++ - 将 lambda 与 Boost Beast 一起使用而不是绑定(bind)

标签 c++ c++11 boost lambda boost-beast

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_codeconnected 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/

相关文章:

c++ - 使用强 typedef 作为 Boost Parameter 库的更轻量级替代方案?

c++ - 从预期的嵌套 block block 访问变量

c++ - 在没有互斥锁的 C++11 中实现共享整数计数器的最简单方法 :

python - Boost.Python 和 Boost.Function

c++ - 当包含 boost/beast/http.hpp 时,CMake 出现多个错误

c++ - 使用 BOOST_PP_SEQ_FOREACH_R 递归处理枚举

c++ - 改进值(value)散列

c++ - Visual Studio 2019 C++-编译时错误地报告错误

C++ 将 unique_ptr 移动到 vector 并继续使用它

c++ - 编译时类型的排列 P(N,R)