将类成员函数作为线程调用到另一个类成员函数时发出警告消息

warning message when calling a class member function inside another as a thread

本文关键字:函数 成员 消息 另一个 警告 调用 线程      更新时间:2023-10-16

我有一个类,它包含多个方法。我的要求是将另一个成员函数中的一个作为线程调用。

Class ApplicationManager
{
....
....
void method();
void test(std::string arg1, std::string& arg2);
};
void ApplicationManager::method()
{
std::string arg, arg2;
....
....
std::thread(&ApplicationManager::test, this, arg, arg2);
}

当我编译以上内容时,我收到了以下警告消息。由于它太大了,我无法理解它想说什么。复制了下面的警告消息。

C:\Program Files(x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\type_traits(16707566):警告C4239:使用了非标准扩展:"argument":从"_Ty"转换为"std::string&"具有[_Ty=std::basic_string,std::分配器>>C: \Program Files(x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\type_traits(16707566):注意:非常量引用只能绑定到左值C: \Program Files(x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\type_traits(16707566):注意:请参阅对函数模板实例化的引用'void std::_Invoker_pm_pointer::_Call&lt_Ty,ApplicationManager,std::basic_string,std:;allocater>,std::basic_string,std::allocater>>(_Decayed,_Ty1&&,std:,basic_strig,std:)noexcept(false)'正在编译具有[_Ty=void(__cdecl ApplicationManager::*)(std::string,std::string&),_衰减=无效(__cdecl ApplicationManager::*)(std::string,std::string&),_Ty1=应用程序管理器*]

C:\Program Files(x86)\Microsoft VisualStudio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\thr/xthread(237):注意:请参阅对函数模板实例化的引用"void"std::invoke,std::allocater>,std::basic_string,std::allocater>>(_Callable&amp;,应用管理服务&amp;,std::basic_string,std::分配器>&amp;,std::basic_string,std::分配器>&amp;)noexcept(false)'正在编译具有[_Callable=void(__cdecl ApplicationManager::)(std::string,std::string&)]C:\Program Files(x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\thr/xthread(246):注意:请参阅对函数模板实例化的引用"void"std::_LaunchPad&lt_目标>::_Execute<0,1,2,3>(std::tuple,std::allocater>,std::basic_string,std::allocater>>和amp;,std:;integer_sequence<_Ty,0,1、2,3>')'正在编译具有[_目标=std::unique_ptr,std::allocater>,std:::basic_string,std:,allocater>>,std:;default_delete,std:,_Ty=尺寸_t]C:\Program Files(x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\thr/xthread(245):注意:请参阅对函数模板实例化的引用"void"std::_LaunchPad&lt_目标>::_Execute<0,1,2,3>(std::tuple,std::allocater>,std::basic_string,std::allocater>>和amp;,std:;integer_sequence<_Ty,0,1、2,3>')'正在编译具有[_目标=std::unique_ptr,std::allocater>,std:::basic_string,std:,allocater>>,std:;default_delete,std:,_Ty=尺寸_t]C:\Program Files(x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\thr/xthread(242):注意:编译类模板成员函数'void时std::_LaunchPad&lt_目标>::Run(std::_LaunchPad<_Target>*)noexcept'具有[_目标=std::unique_ptr,std::allocater>,std:::basic_string,std:,allocater>>,std:;default_delete,std:!allocater>.,std:…basic_string;std::分配器>>>>]C:\Program Files(x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\thr/xthread(230):注意:请参阅对函数模板实例化的引用"void"std::_LaunchPad&lt_目标>::Run(std::_LaunchPad<_Target>*)noexcept'正在编译具有[_目标=std::unique_ptr,std::allocater>,std:::basic_string,std:,allocater>>,std:;default_delete,std:!allocater>.,std:…basic_string;std::分配器>>>>]C:\Program Files(x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\thr/xthread(257):注意:请参阅对类模板实例化的引用'std::_LaunchPad&lt_正在编译目标">"具有[_目标=std::unique_ptr,std::allocater>,std:::basic_string,std:,allocater>>,std:;default_delete,std:!allocater>std::basix_string,std::allocater>>>]C:\Program Files(x86)\Microsoft Visual Studio\2017\Profession\VC\Tools\MSVC\14.16.27023\include\thread(46):注意:请参阅对函数模板实例化的引用"void"std::_Launch,std::allocater>,std::basic_string,std∶:allocater>>,std::default_delete&lt_Ty>>(_Thrd_t*,_目标&amp;)'正在编译具有[_Ty=std::tuple,std::分配器>,std::basic_string,std::分配器>>,_目标=std::unique_ptr,std::allocater>,std::basic_string,std:::allocator>>,std:;default_delete,std:,allocator>,std:。。。。\Sources\ApplicationManager.cpp(3856):注意:请参阅对函数模板实例化的引用'std::thread::thread(_Fn&amp;,ApplicationManager&,std::string&,std::string&)'正在编译具有[_Fn=void(__cdecl ApplicationManager::)(std::string,std::string&)]C:\Program Files(x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\filesystem(2392):注意:请参阅对类模板实例化的引用'std::chrono::time_point'正在编译C:\Program Files(x86)\Microsoft VisualStudio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\type_traits(616):注意:请参阅对类模板实例化的引用'std::basic_string_view>'正在编译的C:\Program Files(x86)\Microsoft VisualStudio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\xstring(2124):注意:请参阅对类模板实例化的引用"std::is_convertable>>"编译的具有[_StringViewIsh=常量wchar_t*]C:\Program Files(x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\xstring(2122):注意:请参阅对变量模板"const bool"的引用conction_v>>,std::negation>>'正在编译的C:\Program文件(x86)\Microsoft VisualStudio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\xstring(2281):注意:请参阅对别名模板实例化的引用'std::basic_string,std::分配器>::_Is_string_view_ish&lt_StringViewIsh>'正在编译具有[_StringViewIsh=常量wchar_t*]*

您的函数

test(std::string arg1, std::string& arg2);

将引用作为第二个参数,但这里是

std::thread(&ApplicationManager::test, this, arg, arg2);

它需要一个值而不是引用,因此您必须将其修改为

std::thread(&ApplicationManager::test, this, arg, std::ref(arg2));

在引用包装器对象中包装对变量的引用。参见

请注意,您发布的编译器消息是特定于MVC编译器的,但您的代码并没有像cigien提到的那样使用gcc或clang进行编译(在编辑之前)。