Is it possible to call function objects created with
std::bind
usingstd::async
Yes, you can call any functor, as long as you provide the right number of arguments.
am I doing something wrong?
You're converting the bound function, which takes no arguments, to a function<int(int,int)>
, which takes (and ignores) two arguments; then trying to launch that with no arguments.
You could specify the correct signature:
function<int()> sumFunc = bind(&Adder::add, &a, 1, 2);
or avoid the overhead of creating a function
:
auto sumFunc = bind(&Adder::add, &a, 1, 2);
or not bother with bind
at all:
auto future = async(launch::async, &Adder::add, &a, 1, 2);
or use a lambda:
auto future = async(launch::async, []{return a.add(1,2);});