这是密码。它不是在vs2013中编译,而是在gcc4.8中编译
错误C2665:‘std::线程::线程’:4种重载都不能转换所有参数类型
由于我使用的是vs2013,有人能提供解决办法吗?
#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);
}发布于 2014-02-07 02:07:27
您可以简单地使用lambda,而不是使用成员函数指针进行猴子操作:
auto aaa = thread( [&]{ A.fun(1); } );
aaa.join();发布于 2015-04-06 06:49:42
如果你不介意的话,还有另外一种方法可以解决上述问题!首先,只需查看线程对象的显式构造函数:
template< class Function, class... Args >
explicit thread( Function&& f, Args&&... args );f -函数对象的通用引用。
args -函数(函子) f的变元参数
(我不打算越来越深入地解释这里使用的各种调用)。因此,现在我们知道可以处理函子了,因此,定义了一个函子(函数对象),如下所示:
template<typename T>
class TestClass
{
public:
TestClass(){};
~TestClass(){};
T t;
template<typename U>
void operator()(U u1,U u2){
std::cout << "fun: " << u1*u2 << '\n';
}
};
int main()
{
TestClass<double> A;
auto aaa = std::thread(A,1,100);// calling functor A(1,100)
aaa.join()
//or if you can move object from main thread to manually created thread aaa ,it's more elegant.
auto aa = std::thread(std::move(A),1,100);
aa.join();
A(1, 99);
system("Pause");
return 0;
}//请注意,这里我没有使用任何储物柜护卫系统。如果使用静态函数,则不必每次都绑定相应的实例,这可能会改变预期的运行时行为,因此必须对其进行管理,
template<typename U>
static void fun(U u)
{
std::cout << "fun: " << u << '\n';
}
then invoke the function,
int main()
{
TestClass<double> A;
auto aaa = std::thread(&TestClass<double>::fun<int>, 1);
system("Pause");
return 0;
}https://stackoverflow.com/questions/21617860
复制相似问题