std :: Invoke,找不到匹配的重载函数

时间:2020-06-04 04:54:53

标签: c++ multithreading std

我正在尝试创建一个线程,该线程使用C ++中的套接字来处理客户端与服务器之间的通信。

程序抛出错误

std :: Invoke,找不到匹配的重载函数

错误C2893无法专用于功能模板'unknown-type std :: invoke(_Callable &&,_ Types && ...)noexcept()'

由于程序在启动时崩溃,因此我无法对其进行调试。

使用两个参数的线程初始化有什么问题吗?还是我缺少一些类导入库?

有人可以帮我吗,我在这里想念什么?


这是我的代码

ResultType

1 个答案:

答案 0 :(得分:1)

我将您的程序简化为minimal, reproducible example

#include <thread>

struct client_type
{
};

int process_client(client_type& new_client)
{
    return 0;
}

int main()
{
    client_type client;

    std::thread my_thread(process_client, client);
}

此小片段无法编译为您提到的错误。

这为什么会失败?让我们看一下std::thread constructor。在注释部分中,我们发现了这一点:

线程函数的参数按值移动或复制。如果 一个引用参数需要传递给线程函数,它具有 要包装(例如,使用std :: ref或std :: cref)。

实际上std::thread my_thread(process_client, std::ref(client));编译没有问题。