Visual C++ 是否支持“严格别名"? [英] Does Visual C++ support "strict aliasing"?

查看:37
本文介绍了Visual C++ 是否支持“严格别名"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近惊讶地发现 C 和 C++ 语言标准有一个严格的别名"规则.本质上,该规则禁止不同类型的变量引用相同的内存位置.

I recently was surprised to learn that the C and C++ language standards have a "strict aliasing" rule. In essence, the rule prohibits variables of differing types from referencing the same memory location.

举个例子:

char buffer[4] = { 0x55, 0x66, 0x77, 0x88 };
int32 *p = (int32*)&buffer[0]; // illegal because buffer[0] and *p are different types

我接触过的大多数专业 C++ 开发人员都不熟悉这条规则.根据我的研究,它似乎主要影响 GCC/G++/CLANG 用户.Visual C++ 是否支持启用/禁用此规则?如果是这样,如何做到这一点?

Most of the professional C++ developers I interact with are not familiar with this rule. Based on my research, it seems to affect mostly GCC/G++/CLANG users. Does Visual C++ support enabling/disabling this rule? If so, how does one do so?

谢谢

推荐答案

严格别名"是限制程序的 C++ 规则,而不是编译器.由于违反规则是未定义行为,不需要诊断编译器不需要以任何方式支持它.

"Strict aliasing" is a C++ rule restricting programs, not compilers. Since violating the rule is Undefined Behavior, no diagnostic required a compiler doesn't need to support it in any way.

也就是说,微软在应用优化方面不太积极.直到上周,他们才宣布他们的新优化器 假设没有签名溢出,GCC 已经假设了几年.严格的别名会破坏一些 Windows 标头,因此需要先修复这些标头.(一些类型就像它们包含 union 一样,但它们并没有正式定义)

That said, Microsoft is a bit less aggressive in applying optimizations. Only last week have they announced their new optimizer assumes no signed overflow, something that GCC has assumed for a few years already. Strict aliasing is going to break a few Windows headers, so those need fixing first. (A few types act as if they contain unions, but they're not formally defined as such)

这篇关于Visual C++ 是否支持“严格别名"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆