【发布时间】:2016-02-14 14:45:57
【问题描述】:
我有一个与模板函数和线程有关的问题:
template <class TYPE_size>
void Threader(TYPE_size counter)
{
counter++;
}
int main()
{
unsigned int counter = 100;
thread one(Threader,counter);
one.join();
cout << counter;
}
这不会编译;我明白了:
错误:没有匹配的调用函数 âstd::thread::thread(, 无符号 int&)✘
如果我删除它编译的模板,并且如果我将函数调用更改为标准函数调用而不是线程(仍在使用模板),它就会编译。
有人知道这是为什么吗?
我使用的是 Centos5 64 位。
error: no matching function for call to âstd::thread::thread(<unresolved overloaded function type>, unsigned int&)â
/usr/lib/gcc/x86_64-redhat-linux6E/4.4.0/../../../../include/c++/4.4.0/thread:124: note: candidates are: std::thread::thread(std::thread&&)
/usr/lib/gcc/x86_64-redhat-linux6E/4.4.0/../../../../include/c++/4.4.0/thread:122: note: std::thread::thread(const std::thread&)
/usr/lib/gcc/x86_64-redhat-linux6E/4.4.0/../../../../include/c++/4.4.0/thread:121: note: std::thread::thread()
【问题讨论】:
-
我不记得参数,类型等,但可能是
thread one(Threader<counter>,counter)? -
根据错误,没有期望函数的线程构造函数。默认、复制、移动就可以了。
标签: c++ multithreading templates c++11