How to use lambda to for boost asio async completion handler

met7
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

void print(boost::asio::deadline_timer* t, int* count)
{
    if (*count < 5)
    {
        std::cout << *count << "\n";
        ++(*count);

        t->expires_at(t->expires_at() + boost::posix_time::seconds(1));
        t->async_wait(boost::bind(print, t, count));
    }
}

int main()
{
    boost::asio::io_service io;

    int count = 0;
    boost::asio::deadline_timer t(io, boost::posix_time::seconds(1));
//    t.async_wait(boost::bind(print, &t, &count));
    t.async_wait([&]{ // compile error occurred
        print(&t, &count);
    });

    io.run();

    std::cout << "Final count is " << count << "\n";

    return 0;
}

What's the different between bind and lambda exp.? I guess it's OK in syntax, the problem is async_wait need a function object with param "const boost::system::error_code& e".

Potatoswatter

I don't know asio very well, but adding the requested parameter fixes the issue.

t.async_wait([&] ( const boost::system::error_code& ) {
    print(&t, &count);
});

It looks like a quirk or bug of Boost.Bind allows additional, ignored arguments to a bind expression generated from a function pointer. It's probably better not to rely on this, but to explicitly accept and discard the error code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use lambda to for boost asio async completion handler

From Dev

rvalue reference to boost asio completion handler

From Dev

Boost::asio::async_read_until never calls handler

From Dev

boost asio priority queue, add async operation from handler

From Dev

How should I use async_read_until and async_write simultaneously in the client app in boost::asio?

From Dev

How to wait for completion of all boost:asio's stackful coroutines?

From Dev

Boost ASIO - What is async

From Dev

How can I use the MDCSnackbarMessage completion handler?

From Dev

What is the use of boost::asio::async_write function

From Dev

Boost asio bind read handler

From Dev

boost::asio custom handler allocation

From Dev

How to use boost::asio with Linux GPIOs

From Dev

How to use port 80 in a boost::asio server?

From Dev

Invalid instance variable in Asio completion handler

From Dev

Boost ASIO UDP client async_receive_from calls handler even when there are no incoming messages

From Dev

Boost ASIO UDP client async_receive_from calls handler even when there are no incoming messages

From Dev

boost::asio how to pass member function to acceptor.async_accept()

From Dev

Boost::Asio Async write failed

From Dev

How to wait for an asio handler?

From Dev

Boost asio TCP async server not async?

From Dev

How to call this completion handler?

From Dev

How to use async lambda with SelectMany?

From Dev

Why boost asio signal handler is immediately canceled?

From Dev

Why boost asio signal handler is immediately canceled?

From Dev

How to use static functions to call UIImageWriteToSavedPhotosAlbum() with completion handler?

From Dev

How to use static functions to call UIImageWriteToSavedPhotosAlbum() with completion handler?

From Dev

How to use boost asio with clang/c2

From Dev

How to use Asio standalone in Xcode C++11 without Boost

From Dev

How to use Asio standalone in Xcode C++11 without Boost

Related Related

  1. 1

    How to use lambda to for boost asio async completion handler

  2. 2

    rvalue reference to boost asio completion handler

  3. 3

    Boost::asio::async_read_until never calls handler

  4. 4

    boost asio priority queue, add async operation from handler

  5. 5

    How should I use async_read_until and async_write simultaneously in the client app in boost::asio?

  6. 6

    How to wait for completion of all boost:asio's stackful coroutines?

  7. 7

    Boost ASIO - What is async

  8. 8

    How can I use the MDCSnackbarMessage completion handler?

  9. 9

    What is the use of boost::asio::async_write function

  10. 10

    Boost asio bind read handler

  11. 11

    boost::asio custom handler allocation

  12. 12

    How to use boost::asio with Linux GPIOs

  13. 13

    How to use port 80 in a boost::asio server?

  14. 14

    Invalid instance variable in Asio completion handler

  15. 15

    Boost ASIO UDP client async_receive_from calls handler even when there are no incoming messages

  16. 16

    Boost ASIO UDP client async_receive_from calls handler even when there are no incoming messages

  17. 17

    boost::asio how to pass member function to acceptor.async_accept()

  18. 18

    Boost::Asio Async write failed

  19. 19

    How to wait for an asio handler?

  20. 20

    Boost asio TCP async server not async?

  21. 21

    How to call this completion handler?

  22. 22

    How to use async lambda with SelectMany?

  23. 23

    Why boost asio signal handler is immediately canceled?

  24. 24

    Why boost asio signal handler is immediately canceled?

  25. 25

    How to use static functions to call UIImageWriteToSavedPhotosAlbum() with completion handler?

  26. 26

    How to use static functions to call UIImageWriteToSavedPhotosAlbum() with completion handler?

  27. 27

    How to use boost asio with clang/c2

  28. 28

    How to use Asio standalone in Xcode C++11 without Boost

  29. 29

    How to use Asio standalone in Xcode C++11 without Boost

HotTag

Archive