std线程调用模板成员函数模板类:编译错误 [英] std thread call template member function of template class: compiler error
本文介绍了std线程调用模板成员函数模板类:编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是代码。它不编译在vs2013,但编译在gcc4.8
Here is the code. It does not compile in vs2013, but does compile in gcc4.8
错误C2665:'std :: thread :: thread':没有一个4重载可以转换所有参数类型
error C2665: 'std::thread::thread' : none of the 4 overloads could convert all the argument types
由于我使用vs2013,任何人都可以提供解决方法?
Since I am using vs2013, can anyone provide workaround?
#include <iostream>
#include <thread>
template<typename T>
class TestClass
{
public:
TestClass(){};
~TestClass(){};
T t;
template<typename U>
void fun(U u)
{
std::cout << "fun: " << u << '\n';
}
};
int main()
{
TestClass<double> A;
auto aaa = std::thread(&TestClass<double>::fun<int>, &A, 1);
}
推荐答案
而不是使用成员函数指针:
You could simply use a lambda rather than monkeying with member function pointers:
auto aaa = thread( [&]{ A.fun(1); } );
aaa.join();
这篇关于std线程调用模板成员函数模板类:编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文