In C++, What does “access” mean in the strict aliasing rule?

耗尽温柔 提交于 2019-12-05 00:30:01

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;
}

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.


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.

I don't claim to be a language lawyer. However...

I would interpret the phrase "access the stored value of an object" as "read the stored value of an object".

That interpretation makes more sense given the previous paragraph, which talks about modifying an object.

9 If an expression can be used to modify the object to which it refers, the expression is called modifiable. A program that attempts to modify an object through a nonmodifiable lvalue or rvalue expression is ill-formed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!