socket.io client std :: bind-错误C2338:元组索引限制

Socket.IO Client std::bind - error C2338: tuple index out of bounds

本文关键字:元组 索引 C2338 bind- io client std socket 错误      更新时间:2023-10-16

我是在https://socket.io/blog/socket-io-cpp/上提供的示例,以创建socket.io client。

我创建了一个通过socket.io处理所有通信的类。我能够成功连接并发送数据。我试图绑定接收_listener以跟踪传入消息。我遇到错误

1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.16.27023includeutility(542): error C2338: tuple index out of bounds
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.16.27023includeutility(559): note: see reference to class template instantiation 'std::tuple_element<0,std::tuple<>>' being compiled
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.16.27023includefunctional(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 studio2017communityvctoolsmsvc14.16.27023includetuple(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 studio2017communityvctoolsmsvc14.16.27023includetuple(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 studio2017communityvctoolsmsvc14.16.27023includetype_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 studio2017communityvctoolsmsvc14.16.27023includefunctional(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 studio2017communityvctoolsmsvc14.16.27023includefunctional(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 studio2017communityvctoolsmsvc14.16.27023includefunctional(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());
}

深色悲伤添加了解决方案,但我想添加一些解释。

查看socket.io的代码,()有两个过载,即

    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 ::占位符:: _我正在用元组扩展时,我们就会走出来。

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

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 ::占位符:: _ 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 ::绑定更容易理解,因为它更接近正常函数定义。

i通过将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)));