在 boost::bind 中使用 boost 信号

发布于 2024-09-24 02:12:18 字数 572 浏览 2 评论 0原文

我正在尝试将 boost::signal 的触发包装到 boost::bind 对象中。所以我想要的是在调用 boost::function 时使用一些预先打包的参数来调用信号。

我所拥有的是:

boost::signals2::signal<void(int)> sig;
boost::function<void()> f = boost::bind(
    &(sig.operator()), &sig, 10);

但这不起作用。我收到以下错误: 错误:没有匹配的函数用于调用绑定(,...

我也尝试过这个:

boost::function<void()> f = boost::bind(
    (void(boost::signals2::signal<void(int)>::*)(int))
    &(sig.operator()), &sig, 10);

但是然后我得到“没有上下文类型信息的重载函数的地址。”

那么正确的语法是什么?

I'm trying to wrap triggering for a boost::signal into a boost::bind object. So what I want is to invoke the signal with some pre-packaged arguments when the boost::function is called.

What I have is this:

boost::signals2::signal<void(int)> sig;
boost::function<void()> f = boost::bind(
    &(sig.operator()), &sig, 10);

But this doesn't work. I get the following error:
error: no matching function for call to bind(, ...

I also tried this:

boost::function<void()> f = boost::bind(
    (void(boost::signals2::signal<void(int)>::*)(int))
    &(sig.operator()), &sig, 10);

But then I get "address of overloaded function with no contextual type information."

So what's the right syntax for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

沙沙粒小 2024-10-01 02:12:18

boost::signals2::signal 的实例是一个函数对象(又名函子),可以直接绑定,如所述 此处。这种情况下唯一的问题是信号是不可复制的,因此不能将其复制到bind 返回的对象中。所以你首先必须用 boost::ref 包装它。这是一个例子:

#include <boost/signals2.hpp>
#include <boost/bind.hpp>
#include <boost/ref.hpp>

int main(void)
{
  boost::signals2::signal<void(int)> sig;
  boost::function<void()> f = boost::bind(boost::ref(sig), 10);
}

An instance of boost::signals2::signal is a function object (a.k.a. functor), and can be bound directly, as described here. The only problem in this case is that a signal is noncopyable, and so it cannot be copied into the object returned by bind. So you first have to wrap it with a boost::ref. Here's an example:

#include <boost/signals2.hpp>
#include <boost/bind.hpp>
#include <boost/ref.hpp>

int main(void)
{
  boost::signals2::signal<void(int)> sig;
  boost::function<void()> f = boost::bind(boost::ref(sig), 10);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文