Is it possible to call function objects created using std::bind using std::async
Yes, you can call any functor if you specified the correct number of arguments.
Am I doing something wrong?
You convert a bound function that takes no arguments to function<int(int,int)> , which takes (and ignores) two arguments; then tries to run it with no arguments.
You can specify the correct signature:
function<int()> sumFunc = bind(&Adder::add, &a, 1, 2);
or avoid overhead when creating a function :
auto sumFunc = bind(&Adder::add, &a, 1, 2);
or don't bind at all:
auto future = async(launch::async, &Adder::add, &a, 1, 2);
or use lambda:
auto future = async(launch::async, []{return a.add(1,2);});
source share