将成员信号绑定到函数
这行代码可以正确编译,没有问题:
boost::bind(boost::ref(connected_),
boost::dynamic_pointer_cast<session<version> >(shared_from_this()),
boost::asio::placeholders::error);
但是,当将其分配给 boost::function 或作为这样的回调时:
socket_->async_connect(connection_->remote_endpoint(),
boost::bind(boost::ref(connected_),
boost::dynamic_pointer_cast<session<version> >(shared_from_this()),
boost::asio::placeholders::error));
我得到一大堆 难以理解的错误(链接是因为它太长,无法放在这里)。
另一方面,我已经成功地将自由信号绑定到 boost::function ,如下所示:
void print(const boost::system::error_code& error)
{
cout << "session connected";
}
int main()
{
boost::signal<void(const boost::system::error_code &)> connected_;
connected_.connect(boost::bind(&print, boost::asio::placeholders::error));
client<>::connection_t::socket_ptr socket_(new client<>::connection_t::socket_t(conn->service())); // shared_ptr of a tcp socket
socket_->async_connect(conn->remote_endpoint(),
boost::bind(boost::ref(connected_),
boost::asio::placeholders::error));
conn->service().run(); // io_service.run()
return 0;
}
这可以正常工作并正确打印 session linked
。 我在这里做错了什么?
This line of code compiles correctly without a problem:
boost::bind(boost::ref(connected_),
boost::dynamic_pointer_cast<session<version> >(shared_from_this()),
boost::asio::placeholders::error);
However when assigning it to a boost::function or as a callback like this:
socket_->async_connect(connection_->remote_endpoint(),
boost::bind(boost::ref(connected_),
boost::dynamic_pointer_cast<session<version> >(shared_from_this()),
boost::asio::placeholders::error));
I'm getting a whole bunch of incomprehensible errors (linked since it's too long to fit here).
On the other hand I have succeeded binding a free signal to a boost::function like this:
void print(const boost::system::error_code& error)
{
cout << "session connected";
}
int main()
{
boost::signal<void(const boost::system::error_code &)> connected_;
connected_.connect(boost::bind(&print, boost::asio::placeholders::error));
client<>::connection_t::socket_ptr socket_(new client<>::connection_t::socket_t(conn->service())); // shared_ptr of a tcp socket
socket_->async_connect(conn->remote_endpoint(),
boost::bind(boost::ref(connected_),
boost::asio::placeholders::error));
conn->service().run(); // io_service.run()
return 0;
}
This works and prints session connected
correctly.
What am I doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否通读过绑定问题排查指南 ?我不熟悉您的编译器,但您的错误似乎与故障排除部分中的第二个项目符号“无法使用指定的参数调用函数对象”类似。
Have you read through the bind troubleshooting guide? I'm not familiar with your compiler, but your error seems similar to the second bullet in the troubleshooting section "The function object cannot be called with the specified arguments."