Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::merge doesn't work with std::async

Tags:

c++

c++11

I want to merge two vectors in a separate thread

int main()
{
    vector<int> a(100);
    vector<int> b(100);
    vector<int> c(200);
    std::async(std::launch::async, std::merge, a.begin(), a.end(), b.begin(),
               b.end(),
               c.begin());
}

This doesn't compile

main.cpp: In function ‘int main()’:
main.cpp:17:25: error: no matching function for call to ‘async(std::launch, <unresolved overloaded function type>, std::vector<int>::iterator, std::vector<int>::iterator, std::vector<int>::iterator, std::vector<int>::iterator, std::vector<int>::iterator)’
                c.begin())
                         ^
In file included from main.cpp:4:0:
/usr/include/c++/6.2.1/future:1709:5: note: candidate: template<class _Fn, class ... _Args> std::future<typename std::result_of<typename std::decay<_Tp>::type(typename std::decay<_BoundArgs>::type ...)>::type> std::async(std::launch, _Fn&&, _Args&& ...)
     async(launch __policy, _Fn&& __fn, _Args&&... __args)
     ^~~~~
/usr/include/c++/6.2.1/future:1709:5: note:   template argument deduction/substitution failed:
main.cpp:17:25: note:   couldn't deduce template parameter ‘_Fn’
                c.begin())
                         ^
In file included from main.cpp:4:0:
/usr/include/c++/6.2.1/future:1739:5: note: candidate: template<class _Fn, class ... _Args> std::future<typename std::result_of<typename std::decay<_Tp>::type(typename std::decay<_BoundArgs>::type ...)>::type> std::async(_Fn&&, _Args&& ...)
     async(_Fn&& __fn, _Args&&... __args)
     ^~~~~
/usr/include/c++/6.2.1/future:1739:5: note:   template argument deduction/substitution failed:
/usr/include/c++/6.2.1/future: In substitution of ‘template<class _Fn, class ... _Args> std::future<typename std::result_of<typename std::decay<_Tp>::type(typename std::decay<_BoundArgs>::type ...)>::type> std::async(_Fn&&, _Args&& ...) [with _Fn = std::launch; _Args = {}]’:
main.cpp:17:25:   required from here
/usr/include/c++/6.2.1/future:1739:5: error: no type named ‘type’ in ‘class std::result_of<std::launch()>’

On the other hand, if I call

std::merge(a.begin(), a.end(), b.begin(), b.end(), c.begin());

Everything works fine. If I use a lambda - too.

Why?

like image 526
marmistrz Avatar asked Dec 16 '16 13:12

marmistrz


1 Answers

std::merge is a template function. Getting a pointer to a template function requires you to explicitly specify what the template parameters are:

std::async(std::launch::async, 
           &std::merge<decltype(a.begin()), decltype(b.begin()), decltype(c.begin())>, 
           a.begin(), a.end(), b.begin(),
           b.end(),
           c.begin());

Using a lambda here is probably the cleanest and more readable solution:

auto merger = [&a, &b, &c]
{ 
    return std::merge(a.begin(), a.end(), b.begin(), b.end(), c.begin());
};

std::async(std::launch::async, merger);

In C++14, you could use a generic lambda to pass the arguments directly in std::async. (It's questionable whether or not it's a better solution than capturing a, b, and c.) Example:

auto merger = [](auto&&... xs)
{ 
    return std::merge(std::forward<decltype(xs)>(xs)...);
};

std::async(std::launch::async, merger, 
           a.begin(), a.end(), b.begin(), b.end(), c.begin());
like image 85
Vittorio Romeo Avatar answered Oct 12 '22 02:10

Vittorio Romeo