Search code examples
c++overloadingstdasync

std::async with overloaded functions


Possible Duplicate:

std::bind overload resolution

Consider following C++ example

class A
{
public:
    int foo(int a, int b);
    int foo(int a, double b);
};

int main()
{
    A a;
    auto f = std::async(std::launch::async, &A::foo, &a, 2, 3.5);
}

This gives 'std::async' : cannot deduce template argument as function argument is ambiguous. How do I resolve this ambiguity??


Solution

  • Help the compiler resolve ambiguity telling which overload you want:

    std::async(std::launch::async, static_cast<int(A::*)(int,double)>(&A::foo), &a, 2, 3.5);
    //                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
    

    or use lambda expression instead:

    std::async(std::launch::async, [&a] { return a.foo(2, 3.5); });