将成员信号绑定到函数

发布于 2024-09-02 09:04:14 字数 1489 浏览 4 评论 0原文

这行代码可以正确编译,没有问题:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜爱纠缠 2024-09-09 09:04:15

您是否通读过绑定问题排查指南 ?我不熟悉您的编译器,但您的错误似乎与故障排除部分中的第二个项目符号“无法使用指定的参数调用函数对象”类似。

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."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文