I looked at the std::async example here :
#include <iostream> #include <vector> #include <algorithm> #include <numeric> #include <future> template <typename RAIter> int parallel_sum(RAIter beg, RAIter end) { auto len = std::distance(beg, end); if(len < 1000) return std::accumulate(beg, end, 0); RAIter mid = beg + len/2; auto handle = std::async(std::launch::async, parallel_sum<RAIter>, mid, end); int sum = parallel_sum(beg, mid); return sum + handle.get(); } int main() { std::vector<int> v(10000, 1); std::cout << "The sum is " << parallel_sum(v.begin(), v.end()) << '\n'; }
I tried to compile it with a web compiler for Clang 3.4, and it led to the conclusion The sum is instead of the expected The sum is 1000 .
I copied this example and compiled with clang 3.5-1ubuntu1 / gcc 4.8 on Ubuntu 14.04.1 64-bit using the following command:
clang++ -g main.cpp -std=c++1y -o out -pthread;
I get the following error:
main.cpp:15:19: error: no matching function for call to 'async' auto handle = std::async(std::launch::async, ^~~~~~~~~~ main.cpp:24:35: note: in instantiation of function template specialization 'parallel_sum<__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > >' requested here std::cout << "The sum is " << parallel_sum(v.begin(), v.end()) << '\n'; ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/future:1523:5: note: candidate template ignored: substitution failure [with _Fn = int (__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > >), _Args = <__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > &, __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > &>]: function cannot return function type 'int (__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > >)' async(launch __policy, _Fn&& __fn, _Args&&... __args) ^ /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/future:1543:5: note: candidate template ignored: substitution failure [with _Fn = std::launch, _Args = <int (__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > >), __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > &, __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > &>]: no type named 'type' in 'std::result_of<std::launch (int (*)(__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > >), __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > &, __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > &)>' async(_Fn&& __fn, _Args&&... __args) ^ 1 error generated. make: *** [all] Error 1
Is this a bug in clang, gcc, libstdc ++ or am i missing something?