【问题标题】:How to solve this async task error in C++如何在 C++ 中解决这个异步任务错误
【发布时间】:2020-06-22 10:56:38
【问题描述】:

在我的 C++ 程序中,我创建了两个重载函数 doSomething() 和 doSomething(int, int)。我的问题是,当我通过传递 (async, doSomething, a, b) 参数调用异步任务时,编译器会给出“No Matching Function for call async”错误。那么伙计们,我怎样才能在异步任务下传递两个 arg doSomething(int, int) 函数。

我的代码:

void doSomething(){ // do some work}
void doSomething(int &a, int &b){ // do some work}

int main(){

int a = 34, b = 44;
auto f = std::async(std::launch::async, doSomething, std::ref(a), std::ref(b));

}

【问题讨论】:

    标签: c++ multithreading c++14


    【解决方案1】:

    问题在于“doSomething”的类型推导。不指定它被推断为“void()”,所以你应该明确指定不同的函数类型来使用:

    auto f = std::async<void(int&,int&)>(std::launch::async, doSomething, std::ref(a), std::ref(b));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-27
      • 2016-05-12
      • 1970-01-01
      • 2021-05-07
      • 2017-04-20
      • 1970-01-01
      相关资源
      最近更新 更多