每个参数都是绝对正确的,但是由于某种原因在 std::invoke 中抛出了异常。
代码:
调用函数:
void AutoSendFunc(
bool& CloseThreads,
char* Request,
int delay,
void(*ProcessFunc)(request),
void(*sendRequest)(char*&, char*))
{
while (!CloseThreads)
{
char* ptr;
sendRequest(ptr, Request);
ProcessFunc(request(std::string(ptr)));
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
delete ptr;
}
}
尝试调用线程:
void sendRequest(char*& recvdest, char*send)
{
}
...
std::vector<std::pair<
std::pair
<std::string,
int>,
void(*)(request)>> Requests;
...
#define Request Requests[i].first.first
char* request = new char[Request.size()+1];
memcpy(request, Request.c_str(), Request.size());
request[Request.size()] = 0;
#undef Request
std::thread thread
(
AutoSendFunc,
CloseThreads,
request,
Requests[i].first.second,
Requests[i].second,
&sendRequest
);
结论:
1>d:\mvs\ide\vc\tools\msvc\14.14.26428\include\thr\xthread(238): error C2672: "std::invoke": не найдена соответствующая перегруженная функция
1>d:\mvs\ide\vc\tools\msvc\14.14.26428\include\thr\xthread(246): note: выполняется компиляция ссылки на экземпляр шаблон функции "void std::_LaunchPad<_Target>::_Execute<0,1,2,3,4,5>(std::tuple<void (__cdecl *)(bool &,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *)),bool,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *)> &,std::integer_sequence<_Ty,0,1,2,3,4,5>)"
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl *)(bool &,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *)),bool,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *)>,std::default_delete<std::tuple<void (__cdecl *)(bool &,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *)),bool,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *)>>>,
1> _Ty=size_t
1> ]
1>d:\mvs\ide\vc\tools\msvc\14.14.26428\include\thr\xthread(245): note: выполняется компиляция ссылки на экземпляр шаблон функции "void std::_LaunchPad<_Target>::_Execute<0,1,2,3,4,5>(std::tuple<void (__cdecl *)(bool &,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *)),bool,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *)> &,std::integer_sequence<_Ty,0,1,2,3,4,5>)"
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl *)(bool &,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *)),bool,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *)>,std::default_delete<std::tuple<void (__cdecl *)(bool &,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *)),bool,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *)>>>,
1> _Ty=size_t
1> ]
1>d:\mvs\ide\vc\tools\msvc\14.14.26428\include\thr\xthread(242): note: при компиляции функции-члена "<Нет данных>" класса класс <Нет данных>
1>d:\mvs\ide\vc\tools\msvc\14.14.26428\include\thr\xthread(230): note: выполняется компиляция ссылки на экземпляр шаблон функции "void std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *) noexcept"
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl *)(bool &,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *)),bool,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *)>,std::default_delete<std::tuple<void (__cdecl *)(bool &,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *)),bool,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *)>>>
1> ]
1>d:\mvs\ide\vc\tools\msvc\14.14.26428\include\thr\xthread(257): note: выполняется компиляция ссылки на экземпляр шаблон класс "std::_LaunchPad<_Target>"
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl *)(bool &,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *)),bool,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *)>,std::default_delete<std::tuple<void (__cdecl *)(bool &,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *)),bool,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *)>>>
1> ]
1>d:\mvs\ide\vc\tools\msvc\14.14.26428\include\thread(46): note: выполняется компиляция ссылки на экземпляр шаблон функции "void std::_Launch<std::unique_ptr<std::tuple<void (__cdecl *)(bool &,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *)),bool,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *)>,std::default_delete<_Ty>>>(_Thrd_t *,_Target &&)"
1> with
1> [
1> _Ty=std::tuple<void (__cdecl *)(bool &,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *)),bool,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *)>,
1> _Target=std::unique_ptr<std::tuple<void (__cdecl *)(bool &,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *)),bool,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *)>,std::default_delete<std::tuple<void (__cdecl *)(bool &,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *)),bool,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *)>>>
1> ]
1>c:\users\alleksh\source\repos\netlib\netlib\netlib.h(152): note: выполняется компиляция ссылки на экземпляр шаблон функции "std::thread::thread<void(__cdecl &)(bool &,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *)),bool&,char*&,_Ty2&,void(__cdecl *&)(request),void(__cdecl *)(char *&,char *),void>(_Fn,bool &,char *&,_Ty2 &,void (__cdecl *&)(request),void (__cdecl *&&)(char *&,char *))"
1> with
1> [
1> _Ty2=int,
1> _Fn=void (__cdecl &)(bool &,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *))
1> ]
1>d:\mvs\ide\vc\tools\msvc\14.14.26428\include\thr\xthread(237): error C2893: Сбой при специализации функции-шаблона "unknown-type std::invoke(_Callable &&,_Types &&...) noexcept(<expr>)"
1>d:\mvs\ide\vc\tools\msvc\14.14.26428\include\thr\xthread(237): note: Со следующими аргументами шаблона:
1>d:\mvs\ide\vc\tools\msvc\14.14.26428\include\thr\xthread(237): note: "_Callable=void (__cdecl *)(bool &,char *,int,void (__cdecl *)(request),void (__cdecl *)(char *&,char *))"
1>d:\mvs\ide\vc\tools\msvc\14.14.26428\include\thr\xthread(237): note: "_Types={bool, char *, int, void (__cdecl *)(request), void (__cdecl *)(char *&,char *)}"
该错误正是由错误地将参数传输到流引起的
CloseThreads
。如果你想为“reference”类型的参数传递一个参数,那么你应该准确地传递std::ref