I have the following class:
class Test
{
public:
void Func1(std::string const& a, std::string const& b, std::function<void(std::vector<double> const&, int)> func);
std::vector<double> Func2(std::vector<double> const& v, size_t const i);
};
To asynchronously call the function Func1 I did:
Test te;
auto fa = std::bind(&Test::Func2, &te, _1, _2);
auto fb = std::bind(&Test::Func1, &te, a, b, fa);
auto fab = std::async(std::launch::async, fb);
The 'fa' and 'fb' compiles but the async call 'fab' doesn't. How should I call the std::async for Func1?
faandfbdon't fully "compile" until you actually call them - the function call operators aren't instantiated till then, it seems. Once you call, you'll realize thatfbis invalid, i.e.fb()won't compile. As soon as you fix that, theasynccall will work :)As a further hint, observe that
std::bindis subtly broken and not recommended for new code. You'd be better served with lambdas (they'd actually work here!).How is bind broken? Like so:
But:
So, you'd be best served by forgetting about
std::bind's existence, and use lambdas:Feel free to mess with it online.