std::async call of member function
The problem appears to be that it won't play nice with member functions. Perhaps you can std::bind
the member function to your object first, before passing it to std::async
:
auto func = std::bind(&Foo::bar, this, std::placeholders::_1);
auto handle = std::async(std::launch::async, func, 0);
I would prefer lambdas to std::bind
#include <iostream>
#include <future>
class Foo
{
private:
void bar(const size_t)
{}
public:
void foo()
{
auto handle = std::async(std::launch::async, [this](){
this->bar(0);
});
handle.get();
}
};
int main()
{
Foo foo;
foo.foo();
return 0;
}
or, but less readable to me,
auto handle = std::async(std::launch::async, [this](const size_t num){
this->bar(num);
}, 0);