socket.io client std :: bind-错误C2338:元组索引限制
Socket.IO Client std::bind - error C2338: tuple index out of bounds
我是在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)));
相关文章:
- 重载元组索引运算符-C++
- 元组的运行时索引
- 为什么我在循环提升时得到意外的索引::hana::元组
- 在 Hana 中满足谓词的元组元素的索引序列
- 打包元组及其索引范围
- C++向量元组,请按索引从元素创建元组
- 为元组实现创建索引序列
- socket.io client std :: bind-错误C2338:元组索引限制
- 如何创建一个 std::tuple,其中包含由索引元组指定的向量中的成员
- 该模板如何工作以找到元组的索引
- 使用boost::引用元组作为orderedunique索引的关键字时出现boost多索引插入错误
- C++11 在运行时不使用 switch 为元组编制索引的方法
- 在运行时获取元组中索引的类型
- 通过索引 C++11 访问元组元素
- 用C++14索引序列可以改进元组变元模板递归吗
- 获取元组元素类型的索引?
- 如何通过地址获取元组中元素的索引
- 在c++中,当元组的元素索引在运行时已知时,是否有可能获得该元素的类型?
- 从元表字段/方法访问数组索引
- 类型索引元组