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?
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); });
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.