boost::bind 违反了严格别名规则?
使用Boost 1.43和GCC 4.4.3,以下代码
boost::bind(&SomeObject::memberFunc, this, _1));
生成以下警告
boost/function/function_base.hpp:321: 警告:取消引用类型双关语 指针将破坏严格别名 规则
在不设置 -fno-strict-aliasing 的情况下消除这些警告的正确方法是什么?
Using Boost 1.43 and GCC 4.4.3, the following code
boost::bind(&SomeObject::memberFunc, this, _1));
Generates the following warning
boost/function/function_base.hpp:321:
warning: dereferencing type-punned
pointer will break strict-aliasing
rules
What's the correct way to eliminate these warnings without setting -fno-strict-aliasing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仅供记录,我对使用 GCC 4.4.3 的 Google Native Client 的 boost::bind 也有同样的警告。 boost从1.41.0版本升级到1.47.0后,该警告消失了。
Just for the record I had the same warning for boost::bind using GCC 4.4.3 for Google's Native Client. The warning disappeared after upgrading boost from version 1.41.0 to 1.47.0.
您确定您拥有与成员函数 foo 所在的类相匹配的正确对象吗?换句话说,在你发布的代码中,*this 的类型与 SomeObject 相同吗?当编译器必须跟踪指向相同原始数据的多个不同类型的指针时,就会发生别名,这就是为什么我怀疑 *this 和 SomeObject 的类型不相同的原因。
Are you sure that you've got the right object matched with the class that the member function foo is in? In other words, in the code you posted, is the type of *this the same as SomeObject? Aliasing occurs when the compiler has to track multiple pointers of different types to the same raw data, which is why I suspect that the type of *this and SomeObject are not the same.