亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: template<typename>不能推導(dǎo)出指針類(lèi)型嗎? [打印本頁(yè)]

作者: asker160    時(shí)間: 2016-06-18 20:01
標(biāo)題: template<typename>不能推導(dǎo)出指針類(lèi)型嗎?
本帖最后由 asker160 于 2016-06-18 20:05 編輯

下面一個(gè)例子程序,編譯運(yùn)行沒(méi)有問(wèn)題。

  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ù)制代碼
但是如果我把f函數(shù)改成模板的形式,如下:

  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. }
復(fù)制代碼
就會(huì)編譯不過(guò),說(shuō)thread::thread構(gòu)造函數(shù)找不到這樣的重載。
error: no matching function for call to 'std::thread::thread(<unresolved overloaded function type>, int*, int*, std::remove_reference<std::promise<int>&>::type)'

這個(gè)錯(cuò)誤是什么含義? 我的模板用法不對(duì)嗎?
謝謝。

作者: windoze    時(shí)間: 2016-06-20 01:18
  1. template<typename Iterator>void f(...)
復(fù)制代碼
這玩意兒不是個(gè)function,是個(gè)function template。




歡迎光臨 Chinaunix (http://72891.cn/) Powered by Discuz! X3.2