1

In this example, if I change this call with bind:

boost::asio::async_connect(
        socket_,
        results.begin(),
        results.end(),
        std::bind(
            &session::on_connect,
            shared_from_this(),
            std::placeholders::_1));

To this:

    auto self = shared_from_this();
    boost::asio::async_connect(
                socket_,
                results.begin(),
                results.end(),
                [self](boost::system::error_code ec) {
        self->on_connect(ec);
    });

I get an assertion error:

boost/boost/asio/impl/connect.hpp:761: error: static_assert failed "IteratorConnectHandler type requirements not met"
  BOOST_ASIO_ITERATOR_CONNECT_HANDLER_CHECK(
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

There's a comment there:

// If you get an error on the following line it means that your handler does
// not meet the documented type requirements for a IteratorConnectHandler.

I personally don't prefer bind, and would like to change it to lambda. Am I doing it wrong or is this a little bug in boost::beast?

By the way changing to a lambda for on_resolve works fine.

The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189

1 Answers1

2

The number of parameters of your lambda doesn't match to handler signature, according to reference async_connect handlers takes error_code and connected endpoint - it is missing in your case.

Fix:

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);
});
rafix07
  • 20,001
  • 3
  • 20
  • 33
  • Perhaps you could replace `boost::asio::ip::tcp::resolver::iterator` with `auto` if you don't care to spell it out or use it – alter_igel Apr 05 '19 at 18:01
  • Yep, you are right, `auto` could be used in this case if OP uses at least C++14. But to provide more self-documented code the full name may be better choice. – rafix07 Apr 05 '19 at 18:16