Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread std::invoke unknown type and failed to specialize function error

I tried to use thread at in function and send parameter as pointer class, i take errors that Error C2672 'std::invoke': no matching overloaded function found and Error C2893 Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...) noexcept(<expr>)'

I search a lot but only i found that about this error everyone forget to send parameter.I did not forget but i still have these errors.

My function as like below

Function Listen

void Listen(can* _c) {
while (_c->s != INVALID_SOCKET)
    {
        auto t = thread( &connect_tcp::Recv_data,_c->connection, _c->s,ref(_c->response), _c->signals ); //ERROR IS HERE
        t.join();
    }
}

Class can

class can {
public:
    Cotp_connection cotp;
    mms mms_obj;
    connect_tcp* connection;
    mms_response response;
    LinkedList** list;
    all_size_encoder size_encoder;
    SOCKET s;
    Signals* signals;


};

Class connect_tcp

class connect_tcp
{
public:

    SOCKET ConnectWithTcp();
    int send_data(SOCKET s, LinkedList** list,int &j);
    connect_tcp();
    connect_tcp(char* ip, int set_port);
    void Close_tcp(SOCKET s);
    void Recv_data(SOCKET s,mms_response &response, Signals *signals);
    std::mutex mutex;

private:
    SOCKET server;
    SOCKADDR_IN addr;
    int port;
    WSADATA WSAData;


};

Function Recv_data

void connect_tcp::Recv_data(SOCKET s,mms_response &response,Signals *signals) {
    LinkedList** list = new LinkedList * [1000];
    uint8_t* buffer = new uint8_t [10000];
    Sleep(1000);
    std::lock_guard<std::mutex>guard(mutex);
    recv(s, (char*)buffer, 10000, 0);
    /*this->mutex.unlock();*/
    decode_bytes(response,buffer, list,signals);

}

At auto t = thread( &connect_tcp::Recv_data,_c->connection, _c->s,ref(_c->response), _c->signals ); connect_tcp::Recv_data is my member function and _c->connection is my object that storage the member function, and other variables are function's parameters. I tried everything that i saw and read but i got these errors everytime, i cannot find to solve this. If you want to explore classes more than my writings. I can edit this post.

Building output

1>------ Build started: Project: kkkkk_v2, Configuration: Debug x64 ------
1>fake_main.cpp
1>C:\Users\serhan.erkovan\source\repos\kkkkk_v2\kkkkk_v2\byte_decode.h(78,94): warning C4138: '*/' found outside of comment
1>C:\Users\serhan.erkovan\source\repos\kkkkk_v2\kkkkk_v2\connect_tcp.cpp(45,3): warning C4551: function call missing argument list
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(40,14): error C2672: 'std::invoke': no matching overloaded function found
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(47): message : see reference to function template instantiation 'unsigned int std::thread::_Invoke<_Tuple,0,1>(void *) noexcept' being compiled
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(47): message :         with
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(47): message :         [
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(47): message :             _Tuple=_Tuple
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(47): message :         ]
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(53): message : see reference to function template instantiation 'unsigned int (__cdecl *std::thread::_Get_invoke<_Tuple,0,1>(std::integer_sequence<unsigned __int64,0,1>) noexcept)(void *)' being compiled
1>C:\Users\serhan.erkovan\source\repos\kkkkk_v2\kkkkk_v2\fake_main.cpp(291): message : see reference to function template instantiation 'std::thread::thread<int(__cdecl &)(SOCKET,int),can*&,void>(_Fn,can *&)' being compiled
1>C:\Users\serhan.erkovan\source\repos\kkkkk_v2\kkkkk_v2\fake_main.cpp(291): message :         with
1>C:\Users\serhan.erkovan\source\repos\kkkkk_v2\kkkkk_v2\fake_main.cpp(291): message :         [
1>C:\Users\serhan.erkovan\source\repos\kkkkk_v2\kkkkk_v2\fake_main.cpp(291): message :             _Fn=int (__cdecl &)(SOCKET,int)
1>C:\Users\serhan.erkovan\source\repos\kkkkk_v2\kkkkk_v2\fake_main.cpp(291): message :         ]
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\chrono(669): message : see reference to class template instantiation 'std::chrono::duration<double,std::ratio<1,1>>' being compiled
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\chrono(177): message : see reference to class template instantiation 'std::chrono::duration<__int64,std::nano>' being compiled
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\chrono(647): message : see reference to class template instantiation 'std::chrono::time_point<std::chrono::steady_clock,std::chrono::steady_clock::duration>' being compiled
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(36,5): error C2893: Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...) noexcept(<expr>)'
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\type_traits(1571): message : see declaration of 'std::invoke'
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(36,5): message : With the following template arguments:
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(36,5): message : '_Callable=int (__cdecl *)(SOCKET,int)'
1>D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(36,5): message : '_Types={can *}'
1>Done building project "kkkkk_v2.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
like image 869
Serhan Erkovan Avatar asked Jun 24 '26 01:06

Serhan Erkovan


1 Answers

The first argument to std::thread is a callable object which is defined like this:

template< class Function, class... Args >
explicit thread( Function&& f, Args&&... args );

This callable object must be callable with this syntax:

f()

But in your example you are passing a member function pointer to the construction of std::thread and the correct syntax to call a member function pointer is like this:

(calleeObject->*fp)()

So you cannot pass a member function pointer to the constructor of std::thread. The easy way to solve it would be using a lambda as your callable object:

std::thread([=_c]()
{
    _c->connection->Recv_data(_c->s, ref(_c->response), _c->signals);
});

or you can use std::mem_fn adapter and include your callee object in your parameters list too as you did it in your own code:

std::thread(std::mem_fn(&connect_tcp::Recv_data), _c->connection, _c->s, ref(_c->response), _c->signals);
like image 69
MRB Avatar answered Jun 25 '26 21:06

MRB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!