在 g++ 中启用严格的别名警告
在 g++ 中启用严格别名警告的正确方法是什么? VC++ 10 执行这些规则吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
在 g++ 中启用严格别名警告的正确方法是什么? VC++ 10 执行这些规则吗?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
对
g++
使用-fstrict-aliasing
。我还使用 -Wstrict-aliasing=2 来查看与可能违反严格别名规则相关的警告。Use
-fstrict-aliasing
forg++
. I also use-Wstrict-aliasing=2
to see warnings related to possible violations of strict aliasing rules.它们由
-O2
自动启用,因为它需要使用它们来进行一些优化。一定要将其与警告结合起来(-Wall
就可以了),以确保您不会构建潜在的错误代码。否则,您可以使用-fstrict-aliasing
(如另一个答案中所示)来启用它们。不过我不确定 VC++10 。
They're enabled automatically by
-O2
because it needs to use them to do some of the optimizations. Definitely combine it with the warning (-Wall
does the trick) to make sure you aren't building potentially buggy code. Otherwise you can use-fstrict-aliasing
as seen in another answer to enable them.I'm not sure about VC++10 however.
VC++ 10 启用 /O1 及以上版本的严格别名规则。我使用 此处。并获得以下 asm 代码。您可以看到 b 的加载仅完成一次。
但似乎没有办法启用违反此规则的警告。
VC++ 10 enables the strict aliasing rule with /O1 and above. I use the test program(with count value 6) in chapter 'BENEFITS TO THE STRICT ALIASING RULE' of here. And get following asm code. You can see the load of b is done only once.
But looks like there isn't a way to enable the warning for breaking this rule.