Reputation: 2383
I have a class and it contains multiple methods. My requirement is to call one of the member function inside another as a thread.
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);
}
When I compile the above, I getting below warning message. Since it is too big, I am unable to get what it tries to say. Copied warning message below.
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\type_traits(16707566): warning C4239: nonstandard extension used: 'argument': conversion from '_Ty' to 'std::string &' with [ _Ty=std::basic_string,std::allocator> ] C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\type_traits(16707566): note: A non-const reference may only be bound to an lvalue C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\type_traits(16707566): note: see reference to function template instantiation 'void std::_Invoker_pmf_pointer::_Call<_Ty,ApplicationManager,std::basic_string,std::allocator>,std::basic_string,std::allocator>>(_Decayed,_Ty1 &&,std::basic_string,std::allocator> &&,std::basic_string,std::allocator> &&) noexcept(false)' being compiled with [ _Ty=void (__cdecl ApplicationManager::* )(std::string,std::string &), _Decayed=void (__cdecl ApplicationManager::* )(std::string,std::string &), _Ty1=ApplicationManager * ]
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\thr/xthread(237): note: see reference to function template instantiation 'void std::invoke,std::allocator>,std::basic_string,std::allocator>>(_Callable &&,ApplicationManager &&,std::basic_string,std::allocator> &&,std::basic_string,std::allocator> &&) noexcept(false)' being compiled with [ _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): note: see reference to function template instantiation 'void std::_LaunchPad<_Target>::_Execute<0,1,2,3>(std::tuple,std::allocator>,std::basic_string,std::allocator>> &,std::integer_sequence<_Ty,0,1,2,3>)' being compiled with [ _Target=std::unique_ptr,std::allocator>,std::basic_string,std::allocator>>,std::default_delete,std::allocator>,std::basic_string,std::allocator>>>>, _Ty=size_t ] C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\thr/xthread(245): note: see reference to function template instantiation 'void std::_LaunchPad<_Target>::_Execute<0,1,2,3>(std::tuple,std::allocator>,std::basic_string,std::allocator>> &,std::integer_sequence<_Ty,0,1,2,3>)' being compiled with [ _Target=std::unique_ptr,std::allocator>,std::basic_string,std::allocator>>,std::default_delete,std::allocator>,std::basic_string,std::allocator>>>>, _Ty=size_t ] C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\thr/xthread(242): note: while compiling class template member function 'void std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *) noexcept' with [ _Target=std::unique_ptr,std::allocator>,std::basic_string,std::allocator>>,std::default_delete,std::allocator>,std::basic_string,std::allocator>>>> ] C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\thr/xthread(230): note: see reference to function template instantiation 'void std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *) noexcept' being compiled with [ _Target=std::unique_ptr,std::allocator>,std::basic_string,std::allocator>>,std::default_delete,std::allocator>,std::basic_string,std::allocator>>>> ] C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\thr/xthread(257): note: see reference to class template instantiation 'std::_LaunchPad<_Target>' being compiled with [ _Target=std::unique_ptr,std::allocator>,std::basic_string,std::allocator>>,std::default_delete,std::allocator>,std::basic_string,std::allocator>>>> ] C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\thread(46): note: see reference to function template instantiation 'void std::_Launch,std::allocator>,std::basic_string,std::allocator>>,std::default_delete<_Ty>>>(_Thrd_t *,_Target &&)' being compiled with [ _Ty=std::tuple,std::allocator>,std::basic_string,std::allocator>>, _Target=std::unique_ptr,std::allocator>,std::basic_string,std::allocator>>,std::default_delete,std::allocator>,std::basic_string,std::allocator>>>> ] ....\Sources\ApplicationManager.cpp(3856): note: see reference to function template instantiation 'std::thread::thread(_Fn &&,ApplicationManager &&,std::string &,std::string &)' being compiled with [ _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): note: see reference to class template instantiation 'std::chrono::time_point' being compiled C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\type_traits(616): note: see reference to class template instantiation 'std::basic_string_view>' being compiled C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\xstring(2124): note: see reference to class template instantiation 'std::is_convertible>>' being compiled with [ _StringViewIsh=const wchar_t * ] C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\xstring(2122): note: see reference to variable template 'const bool conjunction_v > >,std::negation > >' being compiled C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\xstring(2281): note: see reference to alias template instantiation 'std::basic_string,std::allocator>::_Is_string_view_ish<_StringViewIsh>' being compiled with [ _StringViewIsh=const wchar_t * ]*
Upvotes: 0
Views: 816
Reputation: 7100
your function
test(std::string arg1, std::string& arg2);
takes a reference as a second argument but here
std::thread(&ApplicationManager::test, this, arg, arg2);
it takes a value not a reference, hence you have to modify this to
std::thread(&ApplicationManager::test, this, arg, std::ref(arg2));
to wrap a reference to your variable in a reference wrapper object. see
Note that the compiler message you've posted is specific to MVC compiler but your code doesn't compile (before editing) with gcc or clang as cigien has mentioned.
Upvotes: 1