在clang中使用std :: async的模板函数

 wangbiao少爷 发布于 2022-12-29 13:37

我在std::async 这里看一下这个例子,如下:

#include 
#include 
#include 
#include 
#include 

template 
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, mid, end);
    int sum = parallel_sum(beg, mid);
    return sum + handle.get();
}

int main()
{
    std::vector v(10000, 1);
    std::cout << "The sum is " << parallel_sum(v.begin(), v.end()) << '\n';
}

我尝试用Clang 3.4的Web编译器编译它,它导致输出The sum is而不是预期The sum is 1000.

我使用以下命令复制了示例并使用clang 3.5-1ubuntu1/gcc 4.8在Ubuntu 14.04.1 64位上编译:

clang++ -g main.cpp -std=c++1y -o out -pthread;

我收到以下错误:

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 >
      > >' 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 > >,
      __gnu_cxx::__normal_iterator > >), _Args =
      <__gnu_cxx::__normal_iterator > > &,
      __gnu_cxx::__normal_iterator > > &>]:
      function cannot return function type 'int (__gnu_cxx::__normal_iterator > >, __gnu_cxx::__normal_iterator > >)'
    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 =  > >,
      __gnu_cxx::__normal_iterator > >),
      __gnu_cxx::__normal_iterator > > &,
      __gnu_cxx::__normal_iterator > > &>]: no
      type named 'type' in 'std::result_of > >,
      __gnu_cxx::__normal_iterator > >),
      __gnu_cxx::__normal_iterator > > &,
      __gnu_cxx::__normal_iterator > > &)>'
    async(_Fn&& __fn, _Args&&... __args)
    ^
1 error generated.
make: *** [all] Error 1

这是clang,gcc,libstdc ++中的错误,还是我错过了什么?

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有