Where does the standard talk about aliasing?

拜拜、爱过 提交于 2019-12-07 06:04:15

问题


Where in the C++ standard does it talk about aliasing? I looked at the ToC and saw no mention of the word 'alias'. I tried to look in the one definition rule (3.2) and a search of 'alias' had no results there. I'm at a loss where it may be. I'm looking for memory aliasing


回答1:


Aliasing is mainly discussed in §3.10[basic.lval]/10:

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:52

  • the dynamic type of the object,
  • a cv-qualified version of the dynamic type of the object,
  • a type similar (as defined in 4.4) to the dynamic type of the object,
  • a type that is the signed or unsigned type corresponding to the dynamic type of the object,
  • a type that is the signed or unsigned type corresponding to a cv-qualified version of the dynamic type of the object,
  • an aggregate or union type that includes one of the aforementioned types among its elements or non- static data members (including, recursively, an element or non-static data member of a subaggregate or contained union),
  • a type that is a (possibly cv-qualified) base class type of the dynamic type of the object,
  • a char or unsigned char type.

52) The intent of this list is to specify those circumstances in which an object may or may not be aliased.

So, for example,

int x = 1;
*(char*)(&x);     // (implementation-)defined
*(unsigned*)(&x); // (implementation-)defined
*(float*)(&x);    // undefined

The following mentions may also be interesting.

§5.17[expr.ass]/8

If the value being stored in an object is accessed from another object that overlaps in any way the storage of the first object, then the overlap shall be exact and the two objects shall have the same type, otherwise the behavior is undefined. [ Note: This restriction applies to the relationship between the left and right sides of the assignment operation; it is not a statement about how the target of the assignment may be aliased in general. See 3.10. — end note ]

§17.6.4.9[res.on.arguments]/1 pt 3:

If a function argument binds to an rvalue reference parameter, the implementation may assume that this parameter is a unique reference to this argument. … [The implementation is free to optimize away aliasing checks which might be needed if the argument was an lvalue.]

§26.6.2[valarray.syn]/2:

The valarray array classes are defined to be free of certain forms of aliasing, thus allowing operations on these classes to be optimized.

and the rest of <valarray> also discusses how the class should be implemented to avoid data aliasing.



来源:https://stackoverflow.com/questions/9585990/where-does-the-standard-talk-about-aliasing

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