H.Das
H.Das

Reputation: 225

How to solve this async task error in C++

In my C++ program I create two overloading function doSomething() and doSomething(int, int). My problem is that when I call async task by passing (async, doSomething, a, b) parameter the compiler gives "No Matching Function for call async" error. So guys how can I pass two arg doSomething(int, int) function under async task.

My Code:

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));

}

Upvotes: 1

Views: 146

Answers (1)

Equod
Equod

Reputation: 556

The problem is in type deducing for "doSomething". Without specifying it's deduced as "void()" so you should explicitly specify different function type to use:

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

Upvotes: 6

Related Questions