C++ 11:线程创建给我一个"Attempt to use a deleted function"错误



我是线程处理的新手,我对为什么这段代码不起作用感到有点困惑:

// Note that the GKITH object is useful for my whole code
void thread_function( gkit2light_Handler*& GKITH )
{
    std::cout << "t~ Writing from gkit2light thread..." << std::endl;
}
int main( int argc, char** argv )
{
    // Custom gkit2light Handler
    gkit2light_Handler *gkit_handler = new gkit2light_Handler( );
    // Launching thread...
    std::thread gkit_thread( thread_function, gkit_handler );
    return 0;
}

我使用 XCode 9 在 macOS 10.13 下运行它。这些行实际上都没有给我抛出错误,但我的编译器给了我这条消息:"尝试使用已删除的函数"。这很奇怪,因为这里的thread_function((只访问标准输出...

这是已删除的功能,也许这可以帮助您!

struct __nat
{
#ifndef _LIBCPP_CXX03_LANG
    // ...
    // This is the destructor that is throwing the error
    ~__nat() = delete;
#endif
}; 

这是错误的屏幕截图(没有更多信息(:XCode 编译器错误屏幕截图

在将参数传递给thread时,您不能像这样绑定引用(这至少在一个引用中说明(。若要传递引用,请使用 std::ref 包装它。这将在发生在这些参数的移动/复制中幸存下来。

很遗憾,工具链会发出如此神秘的错误消息,但这意味着在其模板元编程中的某个地方,它正在尝试实例化与您编写的代码相关的一些模板,但未能成功。

相关内容

最新更新