Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 不能';t模板<;typename>;推断指针类型?_Multithreading_Templates_C++11_Promise_Future - Fatal编程技术网

Multithreading 不能';t模板<;typename>;推断指针类型?

Multithreading 不能';t模板<;typename>;推断指针类型?,multithreading,templates,c++11,promise,future,Multithreading,Templates,C++11,Promise,Future,我有下面的程序,编译+运行,没问题 #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

我有下面的程序,编译+运行,没问题

#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
}
#包括
#包括
#包括
#包括
void f(int*first,
int*last,
std::承诺累积(U承诺)
{
int sum=std::累加(第一个,最后一个,0);
累积承诺。设置值(总和);//通知未来
}
int main()
{
整数[]={1,2,3,4,5,6};
std::承诺累积\承诺;
std::future acculate\u future=累积承诺。获取未来();
标准:螺纹工作螺纹(f,开始(编号),结束(编号),
标准::移动(累积承诺);
累积未来。等待();//等待结果

std::cout
f
是一个模板

std::thread work_thread(f, begin(numbers), end(numbers),
                        std::move(accumulate_promise));
简而言之,
std::thread
的第一个参数要么是函数指针,要么是类似函数指针的东西。它不将模板作为第一个参数

模板在实例化时成为类或函数。模板在使用时会实例化。因此,给定此模板定义,并以如下方式使用它:

f(something.begin(), something.end(), some_kind_of_a_promise);
这将实例化模板并使用它。要显式实例化模板而不使用它,请执行以下操作:

f<int *>
f
现在,这里有一个实例化的模板。下面的工作如下:

std::thread work_thread(f<int *>, std::begin(numbers),
                        std::end(numbers),
                        std::move(accumulate_promise));
std::thread work\u thread(f,std::begin(数字),
标准::结束(编号),
标准::移动(累积承诺);
按照gcc 5.3.1进行测试

std::thread work_thread(f<int *>, std::begin(numbers),
                        std::end(numbers),
                        std::move(accumulate_promise));