繁体   English   中英

boost不接受匿名函数作为任何输入

[英]boost does not accept anonymous functions as input for anything

以下代码段无法为我编译:

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


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

与错误:

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

我觉得我在这里犯了一个非常愚蠢的错误,但是已经有一段时间了,但我仍然找不到它。

您需要通过引用捕获io_service以获得上述代码片段进行编译:

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

请注意, io_service不实现复制语义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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