将 std::bind 与 boost::signals2 一起使用是否安全?
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在这个主题上没有经验,因为我希望
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.connect
函数采用一个boost::function
对象,它基本上是一个包含operator()< 的任何东西的通用包装器< /code> 为其定义。因此,它与您所绑定的内容一样安全。
例如,这是相当安全的:
这是相当安全的,因为它存储了
boost::shared_ptr
作为其数据的一部分。这是有条件安全的。如果该连接仍然存在并且您执行删除 pValue,它会立即变得不安全。
就我个人而言,我不太相信“有条件安全”,但这取决于你。要点是,只要绑定到
boost::bind
,您绑定的所有内容都必须继续存在。The
connect
function takes aboost::function
object, which is basically a generic wrapper around anything that has anoperator()
defined for it. Therefore it is exactly as safe as what you are binding.For example, this is reasonably safe:
This is reasonably safe because it stores a
boost::shared_ptr
as part of its data.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.