使用std :: async和模板函数

我如何或者能否将模板函数传递给异步?

代码如下:

//main.cpp
#include <future>
#include <vector>
#include <iostream>
#include <numeric>

int
main
    ()
{      
    std::vector<double> v(16,1);

    auto r0 =  std::async(std::launch::async,std::accumulate,v.begin(),v.end(),double(0.0));

    std::cout << r0.get() << std::endl;
    return 0;
}

以下是错误消息:

                                                                               ^
a.cpp:13:88: note: candidates are:
In file included from a.cpp:1:0:
/usr/include/c++/4.8/future:1523:5: note: template std::future::type> std::async(std::launch, _Fn&&, _Args&& ...)
     async(launch __policy, _Fn&& __fn, _Args&&... __args)
     ^
/usr/include/c++/4.8/future:1523:5: note:   template argument deduction/substitution failed:
a.cpp:13:88: note:   couldn't deduce template parameter ‘_Fn’
   auto r0 = std::async(std::launch::async,std::accumulate,v.begin(),v.end(),double(0.0));
                                                                                        ^
In file included from a.cpp:1:0:
/usr/include/c++/4.8/future:1543:5: note: template std::future::type> std::async(_Fn&&, _Args&& ...)
     async(_Fn&& __fn, _Args&&... __args)
     ^
/usr/include/c++/4.8/future:1543:5: note:   template argument deduction/substitution failed:
/usr/include/c++/4.8/future: In substitution of ‘template std::future::type> std::async(_Fn&&, _Args&& ...) [with _Fn = std::launch; _Args = {}]’:
a.cpp:13:88:   required from here
/usr/include/c++/4.8/future:1543:5: error: no type named ‘type’ in ‘class std::result_of’

问题是,要将第二个参数传递给std::async ,编译器必须将表达式&std::accumulate转换为函数指针,但它不知道所需的函数模板的特化。 对于一个人来说,显然你想要一个可以用剩下的参数调用的async ,但是编译器不知道这个并且必须分别评估每个参数。

正如PiotrS的答案所说,你可以通过显式模板参数列表或者使用转换来告诉编译器std::accumulate你想要的,或者你可以直接使用lambda表达式:

std::async(std::launch::async,[&] { return std::accumulate(v.begin(), v.end(), 0.0); });

在lambda体内部,编译器为std::accumulate调用执行重载解析,因此它计算出std::accumulate要使用哪个。


您必须通过显式传递模板参数或使用static_cast来消除可能的实例之间的歧义,如下所示:

auto r0 = std::async(std::launch::async
                     , &std::accumulate<decltype(v.begin()), double>
                     , v.begin()
                     , v.end()
                     , 0.0);

要么:

auto r0 = std::async(std::launch::async
       , static_cast<double(*)(decltype(v.begin()), decltype(v.end()), double)>(&std::accumulate)
       , v.begin()
       , v.end()
       , 0.0);
链接地址: http://www.djcxy.com/p/30747.html

上一篇: using std::async with template functions

下一篇: std::async problems depending on Android version