c++ - std::merge 不适用于 std::async

标签 c++ c++11

我想在一个单独的线程中合并两个 vector

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());
}

这不编译

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()>’

另一方面,如果我调用

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

一切正常。如果我也使用 lambda。

为什么?

最佳答案

std::merge 是一个模板函数。获取指向模板函数的指针需要您明确指定模板参数是什么:

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());

在这里使用 lambda 可能是最干净且更具可读性的解决方案:

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

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

在 C++14 中,您可以使用通用 lambda 直接在 std::async 中传递参数。 (它是否是比捕获 abc 更好的解决方案值得怀疑。) 示例:

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());

关于c++ - std::merge 不适用于 std::async,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41185322/

相关文章:

c++ - 从 vector 文件中读取数据

c++ - 为什么枚举不能是模板?

c++ - 通过作为类的公共(public)成员的两个函数传递一个函数作为参数

c++ - std::map 中 vector 值的迭代器

c++ - 如何用c++求公因数?

c++ - 改进点集的最小距离过滤器

c++ - C++中的顶级是什么?

c++ - 错误 “no matching function for call ' begin(int [len] )' ”在我的代码中是什么意思?

c++ - std::pair 具有可比较对象和不可比较对象需要排序

c++ - 使用一个参数调用宏