boost does not accept anonymous functions as input for anything

144 views Asked by At

The following code piece does not compile for me:

#include <iostream>
#include <boost/thread.hpp>


int main(int argc, char* argv[])
{
  boost::thread thread(
                []() {
                    std::cout<<"hello";
                }
            );
}

With the error :

no matching function for call to ‘boost::thread::thread(main(int, char**)::<lambda()>)’

I feel like I am making a very stupid mistake here, but it has been sometime, and i still fail to find it.

1

There are 1 answers

0
Christian Blume On

You need to capture io_service by reference to get the above code snippet to compile:

void start_thread(boost::asio::io_service &io_service)
{
    boost::thread tcp_thread(
        [&io_service]() {  // <-- you missed a & here
            io_service.run();
        }
    );
}

Note that the io_service does not implement copy semantics.