在C ++中,什么是“访问”在严格的别名规则中的意思是什么? [英] In C++, What does "access" mean in the strict aliasing rule?

查看:147
本文介绍了在C ++中,什么是“访问”在严格的别名规则中的意思是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

3.10 / 10说:

3.10/10 says:


如果程序试图访问除了以下类型之一的glvalue行为未定义:

If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined:

但是,术语访问不在任何地方定义。在这种情况下,是指读取,还是读取或修改

However, the term "access" is not defined anywhere. In this context does it mean read, or read or modify ?

在C标准中,它被明确定义为读取或修改。但是在C ++ 11中,似乎在不同的时间使用不同的含义,例如:

In the C standard it is unambiguously defined as read or modify. However in C++11 it seems to be used with different meanings at different times, for example:

1.9 / 8:


对volatile对象的访问严格按照抽象机器的规则进行评估。

Access to volatile objects are evaluated strictly according to the rules of the abstract machine.

显然,这是要读取或修改,但在许多其他地方,例如1.10 / 24:

Clearly this is meant to be read or modify, however in many other places such as 1.10/24:



  • 访问或修改volatile对象,或

推荐答案

它必须意味着读和写,或者规则并不意味着太多。请考虑 http://blog.llvm .org / 2011/05 / what-every-c-programmer-should-know.html

It must mean both read and write, or the rule wouldn't mean much. Consider the example from http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html:

float *P;
void zero_array() {
   int i;
   for (i = 0; i < 10000; ++i)
       P[i] = 0.0f;
}

上面的示例代码可以优化为 memset 只有当编译器可以假设 P [i] 别名 P 但考虑一个世界,其中只有从不允许的glvalue读取是UB,那么即使 P [i] 别名,上述代码也不会调用UB 对于某些 i - 例如,如果有人 P =(float *)& P; ,因为 P 的所有读取都是完全合法的 - 它们都使用左值表达式

The example code above can be optimized into a memset only if the compiler can assume that none of P[i] aliases P. But consider a world in which only reading from an disallowed glvalue is UB, then the above code would not invoke UB even if P[i] aliases P for some i - e.g., if someone did P = (float *) &P;, because all reads of P are perfectly legal - they all use the lvalue expression P.

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1531rel =nofollow> CWG问题1531 直接点。该问题已于2013年4月移至DR(缺陷报告)状态,但由于某种原因,决议未应用于工作文件。

Edit: CWG issue 1531 is directly on point. The issue was moved to DR (defect report) status in April 2013, but the resolution, for whatever reason, wasn't applied to the working paper.

这篇关于在C ++中,什么是“访问”在严格的别名规则中的意思是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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