【问题标题】:boost does not accept anonymous functions as input for anythingboost 不接受匿名函数作为任何东西的输入
【发布时间】:2015-06-19 18:44:01
【问题描述】:

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

#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
  • 你提供一个Minimal, Complete, Verifiable Example怎么样?
  • 为什么不通过引用捕获?
  • @ÖzümSafaoğlu 这不是一个完整的例子。如果你说不使用io_service,还是报错,那显然也不是minimal
  • 是的,让那些试图免费帮助你的人更轻松,这太荒谬了,真的太离谱了! complete 的含义是否有歧义?您的示例compiles 没有任何错误。请包括您正在使用的编译器、版本、命令行参数和 Boost 版本。

标签: c++ boost anonymous-function boost-thread


【解决方案1】:

需要通过引用捕获io_service才能得到上面的代码sn-p进行编译:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-27
    • 2018-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-10
    • 2015-11-10
    • 2013-02-14
    相关资源
    最近更新 更多