Home > Blockchain >  How to replace std::bind with lambda within steady_timer::async_wait
How to replace std::bind with lambda within steady_timer::async_wait

Time:03-14

Live On Coliru

#include <iostream>
#include <boost/asio.hpp>
#include <functional>

class printer
{
public:
  printer(boost::asio::io_context& io)
    : timer_(io, boost::asio::chrono::seconds(1))
  {
    timer_.async_wait(std::bind(&printer::print, this));
    // timer_.async_wait([this]() {
    //     print();
    // });
  }

  void print()
  { 
      std::cout << "hello world" << std::endl;
  }

private:
  boost::asio::steady_timer timer_;
};

int main()
{
  boost::asio::io_context io;
  printer p(io);
  io.run();

  return 0;
}

I tried to replace the following line:

timer_.async_wait(std::bind(&printer::print, this));

with

 timer_.async_wait([this]() {
     print();
 });

But it failed and the compiler reports the following errors:

main.cpp: In constructor 'printer::printer(boost::asio::io_context&)':
main.cpp:12:22: error: no matching function for call to 'boost::asio::basic_waitable_timer<std::chrono::_V2::steady_clock>::async_wait(printer::printer(boost::asio::io_context&)::<lambda()>)'
   12 |     timer_.async_wait([this]() {
      |     ~~~~~~~~~~~~~~~~~^~~~~~~~~~~
   13 |         print();
      |         ~~~~~~~~      
   14 |     });
      |     ~~  

Question> What is the correct way to rewrite the std::bind with lambda expression?

Thank you

CodePudding user response:

Your lambda doesn't have the required signature.

Based the boost documentation, the callback takes a const boost::system::error_code& argument. std::bind lets you be a bit looser in the function signature (Why std::bind can be assigned to argument-mismatched std::function?), but lambdas need to be an exact match.

The following code seems to work for me

    timer_.async_wait([this](auto const &ec) {
        print();
    });
  • Related