Reputation: 117
I thought this would be simpler but having tried many variations of the following I can't get this code to compile
#include <thread>
#include <algorithm>
#include <future>
int main()
{
std::vector<int> vec(1000000, 0);
std::future<int> x = std::async(std::launch::async, std::accumulate, vec.begin(), vec.end(), 0);
}
error: no matching function for call to 'async(std::launch, <unresolved overloaded function type>, std::vector<int>::iterator, std::vector<int>::iterator, int)'
What am I missing?
Upvotes: 3
Views: 361
Reputation: 48605
Because std::accumulate
is a template you have to supply the template parameters (to resolve it to a specific function) before taking its address.
#include <thread>
#include <algorithm>
#include <future>
#include <vector>
#include <numeric>
int main()
{
std::vector<int> vec(1000000, 0);
std::future<int> x = std::async(std::launch::async,
&std::accumulate<std::vector<int>::const_iterator, int>,
vec.begin(), vec.end(), 0);
}
That's kind of yuck so you could use a lambda instead:
std::future<int> x = std::async(std::launch::async,
[&]{ return std::accumulate(vec.begin(), vec.end(), 0); });
Upvotes: 4