Socket.IO客户端std :: bind - 错误C2338:元组索引超出范围

问题描述 投票:0回答:2

我指的是在https://socket.io/blog/socket-io-cpp/上提供的示例来创建一个Socket.IO客户端。

我创建了一个通过Socket.IO处理所有通信的类。我能够成功连接和发送数据。我试图绑定receive_listener来跟踪传入的消息。我收到了错误

1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\utility(542): error C2338: tuple index out of bounds
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\utility(559): note: see reference to class template instantiation 'std::tuple_element<0,std::tuple<>>' being compiled
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\functional(1832): note: see reference to class template instantiation 'std::tuple_element<1,std::tuple<sio::event &>>' being compiled
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\tuple(958): note: see reference to alias template instantiation 'std::tuple_element_t<1,std::tuple<sio::event &>>' being compiled
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\tuple(987): note: see reference to function template instantiation 'const tuple_element<_Index,std::tuple<_Rest...>>::type &&std::get(const std::tuple<_Rest...> &&) noexcept' being compiled
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\type_traits(1871): note: see reference to alias template instantiation 'std::_Is_invocable_r_<void,std::_Binder<std::_Unforced,void (__cdecl Socket_IO::* )(const std::string &,const sio::message::ptr &,bool,sio::message::list &),Socket_IO *,const std::_Ph<1> &,const std::_Ph<2> &,const std::_Ph<3> &,const std::_Ph<4> &>&,sio::event&>' being compiled
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\functional(1277): note: see reference to class template instantiation 'std::_Is_invocable_r<_Ret,_Fx &,sio::event &>' being compiled
1>        with
1>        [
1>            _Ret=void,
1>            _Fx=std::_Binder<std::_Unforced,void (__cdecl Socket_IO::* )(const std::string &,const sio::message::ptr &,bool,sio::message::list &),Socket_IO *,const std::_Ph<1> &,const std::_Ph<2> &,const std::_Ph<3> &,const std::_Ph<4> &>
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\functional(1277): note: see reference to variable template 'const bool conjunction_v<std::negation<std::is_same<std::_Binder<std::_Unforced,void (__cdecl Socket_IO::*)(std::basic_string<char,std::char_traits<char>,std::allocator<char> > const &,std::shared_ptr<sio::message> const &,bool,sio::message::list &),Socket_IO *,std::_Ph<1> const &,std::_Ph<2> const &,std::_Ph<3> const &,std::_Ph<4> const &>,std::function<void __cdecl(sio::event &)> > >,std::_Is_invocable_r<void,std::_Binder<std::_Unforced,void (__cdecl Socket_IO::*)(std::basic_string<char,std::char_traits<char>,std::allocator<char> > const &,std::shared_ptr<sio::message> const &,bool,sio::message::list &),Socket_IO *,std::_Ph<1> const &,std::_Ph<2> const &,std::_Ph<3> const &,std::_Ph<4> const &> &,sio::event &> >' being compiled
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\functional(1499): note: see reference to alias template instantiation 'std::_Func_class<_Ret,sio::event &>::_Enable_if_callable_t<_Fx&,std::function<void (sio::event &)>>' being compiled
1>        with
1>        [
1>            _Ret=void,
1>            _Fx=std::_Binder<std::_Unforced,void (__cdecl Socket_IO::* )(const std::string &,const sio::message::ptr &,bool,sio::message::list &),Socket_IO *,const std::_Ph<1> &,const std::_Ph<2> &,const std::_Ph<3> &,const std::_Ph<4> &>
1>        ]

每当我添加该行

this->current_socket->on("message", std::bind(&Socket_IO::receive_listener, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));

当我删除上面一行时,程序开始工作。但是,上述行必须绑定接收侦听器以跟踪传入消息。

void Socket_IO::connection_listener(void)
{
    this->current_socket = this->client.socket();
    this->connectionEstablished = true;
    this->current_socket->on("message", std::bind(&Socket_IO::receive_listener, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4)); //The line giving error
    this->sendMessage("Hellowm, World!!! Programing Socket.IO");
}

void Socket_IO::receive_listener(std::string const&name, sio::message::ptr const& data, bool hasAck, sio::message::list &ack_resp)
{
    spdlog::info("Data Recieved : " + data->get_map()["message"]->get_string());
    this->recvDataQueue.push(data->get_map()["message"]->get_string());
}
c++
2个回答
1
投票

Dark Sorrow添加了解决方案,但我想补充一些解释。

查看Socket.IO的代码,on()有两个重载,即

    void on(std::string const& event_name,event_listener const& func);

    void on(std::string const& event_name,event_listener_aux const& func);

,带有类型定义

    typedef std::function<void(const std::string& name,message::ptr const& message,bool need_ack, message::list& ack_message)> event_listener_aux;

    typedef std::function<void(event& event)> event_listener;

什么时候做

this->current_socket->on("message", std::bind(&Socket_IO::receive_listener, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));

编译器不知道选择哪个重载,因为std :: bind不返回event_lister和event_lister_aux的对象。不知何故,event_lister被选中。但是另一个重载没有足够的参数,所以当std :: placeholders :: _ i用元组扩展时,我们就会超出界限。

解决方案是明确告诉编译器您想要哪个重载

this->current_socket->on("message", 
    sio::socket::event_listener_aux(
        std::bind(&Socket_IO::receive_listener, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4)
));

现在我们有了解决方法。但我们不应该高兴,因为std :: bind的语法令人困惑(成员函数指针,std :: placeholders :: _ i,...)。

最好在这里使用lambda,即使参数列表很长

this->current_socket->on("message", 
    sio::socket::event_listener_aux([this](std::string const& name, sio::message::ptr const& data, bool hasAck, sio::message::list &ack_resp)
     { 
         receive_listener(name, data, hasAck, ack_resp); 
     }
);

。这比std :: bind更容易理解,因为它更接近正常的函数定义。


0
投票

我通过将sio::socket::event_listener_aux变量传递给重载的on方法而不是直接绑定来解决了这个问题。

sio::socket::event_listener_aux event_listener_aux
this->event_listener_aux = std::bind(&Socket_IO::receive_listener, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4);
this->current_socket->on("message", event_listener_aux);

要么

this->current_socket->on("message", sio::socket::event_listener_aux(std::bind(&Socket_IO::receive_listener, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4)));
© www.soinside.com 2019 - 2024. All rights reserved.