gpt4 book ai didi

c++ - std线程调用模板类: compiler error的模板成员函数

转载 作者:太空狗 更新时间:2023-10-29 23:51:22 25 4
gpt4 key购买 nike

这是代码。它在vs2013中不编译,但在gcc4.8中编译

错误 C2665:“std::thread::thread”: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);
}

最佳答案

您可以简单地使用 lambda 而不是使用成员函数指针进行修改:

auto aaa = thread( [&]{ A.fun(1); } );
aaa.join();

关于c++ - std线程调用模板类: compiler error的模板成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21617860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com