asker160 发表于 2016-06-18 20:01

template<typename>不能推导出指针类型吗?

本帖最后由 asker160 于 2016-06-18 20:05 编辑

下面一个例子程序,编译运行没有问题。
#include <thread>
#include <future>
#include <iostream>
#include <algorithm>
void f(int* first,
       int* last,
       std::promise<int> accumulate_promise)
{
    int sum = std::accumulate(first, last, 0);
    accumulate_promise.set_value(sum);// Notify future
}

int main()
{
    int numbers[] = { 1, 2, 3, 4, 5, 6 };
    std::promise<int> accumulate_promise;
    std::future<int> accumulate_future = accumulate_promise.get_future();
    std::thread work_thread(f, begin(numbers), end(numbers),
                            std::move(accumulate_promise));
    accumulate_future.wait();// wait for result
    std::cout << "result=" << accumulate_future.get() << '\n';
    work_thread.join();// wait for thread completion
}
但是如果我把f函数改成模板的形式,如下:
template<typename Iterator>
void f(Iterator first,
       Iterator last,
       std::promise<int> accumulate_promise)
{
    int sum = std::accumulate(first, last, 0);
    accumulate_promise.set_value(sum);// Notify future
}
就会编译不过,说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)'

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

windoze 发表于 2016-06-20 01:18

template<typename Iterator>void f(...)这玩意儿不是个function,是个function template。
页: [1]
查看完整版本: template<typename>不能推导出指针类型吗?