将 std::bind 与 boost::signals2 一起使用是否安全?

发布于 2024-12-08 18:00:03 字数 171 浏览 0 评论 0原文

使用 std::bind 将成员函数传递给 boost::signals2::signal::connect() 是否安全?换句话说,boost::bind 和 std::bind 可以互换吗?

它使用 VC++ 2010 SP1 进行编译,但模板代码超出了我的能力范围,我担心我可能会冒险进入未定义的行为领域。

Is it safe to use std::bind to pass a member function to boost::signals2::signal::connect()? In other words, is boost::bind and std::bind interchangeable?

It compiles with VC++ 2010 SP1, but the template code is way over my head and I'm afraid I might be venturing into undefined behaviour territory.

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

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

发布评论

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

评论(2

枕梦 2024-12-15 18:00:03

我在这个主题上没有经验,因为我希望 connect 能够采取任何实现有效函数调用运算符的东西。使用与签名匹配的任何函数或函数对象调用它应该是安全的,无论是 boost::bind、std::bind 还是其他任何东西。 Boost 库被设计为通用的,因此它们通常不会选择彼此的实现细节。

I'm not experienced in the subject by I would expect connect to take anything that implements a valid function call operator. It should be safe to call it with any function or function object that matches the signature, be it boost::bind, std::bind or anything else. Boost libraries are designed to be generic, so they usually don't go picking each others implementation details.

合久必婚 2024-12-15 18:00:03

connect 函数采用一个 boost::function 对象,它基本上是一个包含 operator()< 的任何东西的通用包装器< /code> 为其定义。因此,它与您所绑定的内容一样安全。

例如,这是相当安全的:

boost::shared_ptr<ClassName> pValue = boost::make_shared<ClassName>(...);
signal.connect(boost::bind(&ClassName::FuncName, pValue, ...);

这是相当安全的,因为它存储了 boost::shared_ptr 作为其数据的一部分。

ClassName *pValue = new ClassName(...);
signal.connect(boost::bind(&ClassName::FuncName, pValue, ...);

这是有条件安全的。如果该连接仍然存在并且您执行删除 pValue,它会立即变得不安全。

就我个人而言,我不太相信“有条件安全”,但这取决于你。要点是,只要绑定到 boost::bind ,您绑定的所有内容都必须继续存在。

The connect function takes a boost::function object, which is basically a generic wrapper around anything that has an operator() defined for it. Therefore it is exactly as safe as what you are binding.

For example, this is reasonably safe:

boost::shared_ptr<ClassName> pValue = boost::make_shared<ClassName>(...);
signal.connect(boost::bind(&ClassName::FuncName, pValue, ...);

This is reasonably safe because it stores a boost::shared_ptr as part of its data.

ClassName *pValue = new ClassName(...);
signal.connect(boost::bind(&ClassName::FuncName, pValue, ...);

This is conditionally safe. It instantly becomes unsafe if that connection still exists and you execute delete pValue.

Personally, I don't put much faith in "conditionally safe", but that's up to you. The point being that everything you bind to boost::bind must continue to exist so long as it is bound.

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