模板<typename> 不能推断出指针类型吗?
Couldn't template<typename> deduce pointer type?
我有如下程序,编译+运行,没问题
#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
}
然后编译失败,gcc 报告 thread::thread() ctor 找不到合适的重载:
错误:没有匹配函数来调用 'std::thread::thread(, int*, int*, std::remove_reference&>::type)'
消息表示我的模板有什么问题吗?
如何解决?
谢谢。
f
是模板。
std::thread work_thread(f, begin(numbers), end(numbers),
std::move(accumulate_promise));
通俗地说,std::thread
的第一个参数要么是函数指针,要么是类似于函数指针的东西。它不以模板作为第一个参数。
一个模板在实例化时变成了一个class,或者一个函数。模板在使用时被实例化。所以,给定这个模板定义,并以这样的方式使用它:
f(something.begin(), something.end(), some_kind_of_a_promise);
这会实例化一个模板,并使用它。要显式实例化模板,而不使用它:
f<int *>
现在,您在这里有一个实例化的模板。以下作品在这里:
std::thread work_thread(f<int *>, std::begin(numbers),
std::end(numbers),
std::move(accumulate_promise));
使用 gcc 5.3.1 测试
我有如下程序,编译+运行,没问题
#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
}
然后编译失败,gcc 报告 thread::thread() ctor 找不到合适的重载: 错误:没有匹配函数来调用 'std::thread::thread(, int*, int*, std::remove_reference&>::type)'
消息表示我的模板有什么问题吗? 如何解决?
谢谢。
f
是模板。
std::thread work_thread(f, begin(numbers), end(numbers),
std::move(accumulate_promise));
通俗地说,std::thread
的第一个参数要么是函数指针,要么是类似于函数指针的东西。它不以模板作为第一个参数。
一个模板在实例化时变成了一个class,或者一个函数。模板在使用时被实例化。所以,给定这个模板定义,并以这样的方式使用它:
f(something.begin(), something.end(), some_kind_of_a_promise);
这会实例化一个模板,并使用它。要显式实例化模板,而不使用它:
f<int *>
现在,您在这里有一个实例化的模板。以下作品在这里:
std::thread work_thread(f<int *>, std::begin(numbers),
std::end(numbers),
std::move(accumulate_promise));
使用 gcc 5.3.1 测试