boost::asio::async_read bind compilation error

Deadly 提交于 2019-12-06 15:08:06

Your completion handler signature is not correct, consider this example

#include <boost/asio.hpp>

#include <boost/function.hpp>
#include <boost/bind.hpp>

void
foo()
{

}

int
main()
{
    boost::asio::io_service io_service;
    boost::asio::ip::tcp::socket socket( io_service );

    char buf[2];

    // this compiles file
    boost::asio::async_read(
            socket,
            boost::asio::buffer(buf),
            boost::asio::transfer_at_least(2),
            boost::bind( &foo )
            );

    // this does not
    boost::function<void()> cb = boost::bind( &foo );
    boost::asio::async_read(
            socket,
            boost::asio::buffer(buf),
            boost::asio::transfer_at_least(2),
            cb
            );

}

boost::bind is smart enough to not pass the error or bytes_transferred parameters to your bound function pointer. The author of the Asio library has a detailed blog post about using bind with the library. It is worth the read.

The async_* operations requires a different signature for the callback function:

void handler(
  const boost::system::error_code& error, // Result of operation.
  std::size_t bytes_transferred           // Number of bytes read.
); 

Please have a deeper look at the documentation for some examples how to write and invoke such a callback handler.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!