免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2381 | 回复: 1

[C++] template<typename>不能推导出指针类型吗? [复制链接]

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:58:11
发表于 2016-06-18 20:01 |显示全部楼层
本帖最后由 asker160 于 2016-06-18 20:05 编辑

下面一个例子程序,编译运行没有问题。

  1. #include <thread>
  2. #include <future>
  3. #include <iostream>
  4. #include <algorithm>
  5. void f(int* first,
  6.        int* last,
  7.        std::promise<int> accumulate_promise)
  8. {
  9.     int sum = std::accumulate(first, last, 0);
  10.     accumulate_promise.set_value(sum);  // Notify future
  11. }

  12. int main()
  13. {
  14.     int numbers[] = { 1, 2, 3, 4, 5, 6 };
  15.     std::promise<int> accumulate_promise;
  16.     std::future<int> accumulate_future = accumulate_promise.get_future();
  17.     std::thread work_thread(f, begin(numbers), end(numbers),
  18.                             std::move(accumulate_promise));
  19.     accumulate_future.wait();  // wait for result
  20.     std::cout << "result=" << accumulate_future.get() << '\n';
  21.     work_thread.join();  // wait for thread completion
  22. }
复制代码
但是如果我把f函数改成模板的形式,如下:

  1. template<typename Iterator>
  2. void f(Iterator first,
  3.        Iterator last,
  4.        std::promise<int> accumulate_promise)
  5. {
  6.     int sum = std::accumulate(first, last, 0);
  7.     accumulate_promise.set_value(sum);  // Notify future
  8. }
复制代码
就会编译不过,说thread::thread构造函数找不到这样的重载。
error: no matching function for call to 'std::thread::thread(<unresolved overloaded function type>, int*, int*, std::remove_reference<std::promise<int>&>::type)'

这个错误是什么含义? 我的模板用法不对吗?
谢谢。

论坛徽章:
44
15-16赛季CBA联赛之浙江
日期:2021-10-11 02:03:59程序设计版块每日发帖之星
日期:2016-07-02 06:20:0015-16赛季CBA联赛之新疆
日期:2016-04-25 10:55:452016科比退役纪念章
日期:2016-04-23 00:51:2315-16赛季CBA联赛之山东
日期:2016-04-17 12:00:2815-16赛季CBA联赛之福建
日期:2016-04-12 15:21:2915-16赛季CBA联赛之辽宁
日期:2016-03-24 21:38:2715-16赛季CBA联赛之福建
日期:2016-03-18 12:13:4015-16赛季CBA联赛之佛山
日期:2016-02-05 00:55:2015-16赛季CBA联赛之佛山
日期:2016-02-04 21:11:3615-16赛季CBA联赛之天津
日期:2016-11-02 00:33:1215-16赛季CBA联赛之浙江
日期:2017-01-13 01:31:49
发表于 2016-06-20 01:18 |显示全部楼层
  1. template<typename Iterator>void f(...)
复制代码
这玩意儿不是个function,是个function template。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP