简体   繁体   中英

boost does not accept anonymous functions as input for anything

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM