用C C99严格别名规则++(GCC) [英] C99 strict aliasing rules in C++ (GCC)

查看:417
本文介绍了用C C99严格别名规则++(GCC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,GCC支持C中的所有它的C99功能++。但是如何C99严格别名在C code处理?

As far as I understand, GCC supports all of its C99 features in C++. But how is C99 strict aliasing handled in C++ code?

我知道,用C铸造无关类型之间蒙上不严格走样安全的,并且可能会产生不正确code,但对于C ++?由于严格别名不是C ++标准的一部分(是正确的?),GCC必须是指定的语义本身。

I know that casting with C casts between unrelated types is not strict-aliasing-safe and may generate incorrect code, but what about C++? Since strict aliasing is not part of C++ standard (is that correct?), GCC must be specifying the semantics itself.

我的身影的const_cast 的static_cast 相关类型之间的转换,因此,他们是安全的,而 reinter pret_cast 可以打破严格走样规则。

I figure const_cast and static_cast cast between related types, hence they are safe, while reinterpret_cast can break strict aliasing rules.

这是一个正确的认识?

推荐答案

没有,你可能混合使用不同的东西。

No, you are probably mixing different things.

严格别名规则绝对无关,与C99标准明确。严格别名规则的根源在于均C和C ++自[标准化]次年初present标准的组成部分。禁止通过另一种类型的左值访问一个类型的对象,该条款是present C89 / 90( 6.3 ),以及在C ++ 98( 3.10 / 15 )。这就是严格别名是一回事,没有更多,不会少。这只是,并不是所有的编译器想要(或不敢)执行,或依赖于它。 C和C ++语言有时被作为高级别大会的语言和严格别名规则通常有这样的用途干涉。这是GCC,使得这大胆的举动,并决定开始依托优化严格别名规则,往往与集​​结号类型的图纸投诉。

Strict aliasing rules have absolutely nothing to do with C99 standard specifically. Strict aliasing rules are rooted in parts of the standard that were present in C and C++ since the beginning of [standardized] times. The clause that prohibits accessing object of one type through a lvalue of another type is present in C89/90 (6.3) as well as in C++98 (3.10/15). That's what strict aliasing is all about, no more, no less. It is just that not all compilers wanted (or dared) to enforce it or rely on it. Both C and C++ languages are sometimes used as "high-level assembly" languages and strict aliasing rules often interfere with such uses. It was GCC that made that bold move and decided to start relying on strict aliasing rules in optimizations, often drawing complaints from those "assembly" types.

这是事实,打破严格走样规则在C ++中最直接的方法就是 reinter pret_cast (和C风格的演员,当然)。然而,的static_cast ,也可以用于这个目的,因为它允许人们通过使用无效* 以打破严格走样中间型的链接投

It is true that the most straightforward way to break strict aliasing rules in C++ is reinterpret_cast (and C-style cast, of course). However, static_cast can also be used for that purpose, since it allows one to break strict aliasing by using void * as an intermediate type in a "chained" cast

int *pi;
...
double *pd = static_cast<double *>(static_cast<void *>(pi));

的const_cast 在兼容的编译器不能打破严格走样。

const_cast cannot break strict aliasing in a compliant compiler.

至于C99 ... C99什么都介绍是限制预选赛。这直接关系到走样,但它不是所谓的严格别名本身

As for C99... What C99 did introduce was the restrict qualifier. This is directly related to aliasing, but it is not what is known as strict aliasing per se.

这篇关于用C C99严格别名规则++(GCC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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