Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

288
Visualizações
How to replace std::bind with lambda within steady_timer::async_wait

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

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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();
    });
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda