- 論壇徽章:
- 1
|
本帖最后由 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函數(shù)改成模板的形式,如下:
- 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構造函數(shù)找不到這樣的重載。
error: no matching function for call to 'std::thread::thread(<unresolved overloaded function type>, int*, int*, std::remove_reference<std::promise<int>&>::type)'
這個錯誤是什么含義? 我的模板用法不對嗎?
謝謝。
|
|