模板函数在作为线程调用时不会编译 [英] Template function will not compile when called as a thread

查看:366
本文介绍了模板函数在作为线程调用时不会编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有与模板函数和线程有关的问题:

I have a problem relating to template functions and threads:

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(,unsigned
int&)â

error: no matching function for call to âstd::thread::thread(, unsigned int&)â

如果我删除模板,它编译,如果我将函数调用更改为标准函数调用而不是

If I remove the template it compiles and if I change the function call to a standard function call rather than a thread (still using the template) it compiles.

有人知道为什么会这样吗?

Does anyone know why this is?

使用Centos5 64位。

I'm using Centos5 64 bit.

 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()


推荐答案

没有名为Threader的函数。当你写 Threader< int> 或某事时,编译器创建一个函数。如果你写 Threader< float> ,那么编译器创建一个新的函数。您可以提供默认模板参数,也可以在调用时提供参数。

There is no function named Threader. When you write Threader<int> or something, then the compiler creates a function. If you then write Threader<float>, then the compiler creates a new function. You can either provide a default template parameter, or give it a parameter when you call it.

template <class TYPE_size=int>

thread one(Threader<int>, counter);

这篇关于模板函数在作为线程调用时不会编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆