gpt4 book ai didi

c++ - Socket.IO 客户端 std::bind - 错误 C2338:元组索引越界

转载 作者:行者123 更新时间:2023-11-28 04:22:34 25 4
gpt4 key购买 nike

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

我创建了一个类来处理所有通过 Socket.IO 进行的通信。我能够成功连接并发送数据。我正在尝试绑定(bind) 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));

当我删除上面的行时,程序开始运行。但是,以上行对于绑定(bind)接收监听器以跟踪传入消息是必需的。

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());
}

最佳答案

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 更容易理解,因为它更接近于正常的函数定义。

关于c++ - Socket.IO 客户端 std::bind - 错误 C2338:元组索引越界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55095155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com