Std :: async with overloaded functions

Possible duplicate:

std :: bind overload

Consider the 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 output the template argument, since the function argument is ambiguous. How to eliminate this ambiguity?

+7
c ++ method-overloading stdasync
source share
2 answers

Help the compiler eliminate the ambiguity by indicating which overload you want:

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

or use the lambda expression instead:

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

Using std :: bind overload permission I will figure out a solution for my question. There are two ways to do this (for me).

  • Using std::bind

     std::function<int(int,double)> func = std::bind((int(A::*)(int,double))&A::foo,&a,std::placeholders::_1,std::placeholders::_2); auto f = std::async(std::launch::async, func, 2, 3.5); 
  • Directly using function binding

     auto f = std::async(std::launch::async, (int(A::*)(int, double))&A::foo, &a, 2, 3.5) 
+2
source share

All Articles