Will accessing a class object through a pointer to its derived class violate strict alias rules?

void foobar(Base* base) { Derived* derived = dynamic_cast<Derived*>(base); // or static_cast derived->blabla = 0xC0FFEE; if (base->blabla == 0xC0FFEE) ... } 

In compilers with a strict alias , is the alias for the "base" output?

+6
c ++
source share
1 answer

Two pointers are smoothed when you can access the same object through them. Section 3.10 / 15 of the standard indicates when access to an object is valid.

If the program tries to access the stored value of the object through an l value other than one of the following types, the behavior is undefined:

  • dynamic type of object
  • cv-qualified version of the dynamic type of an object,
  • a type that is a signed or unsigned type corresponding to a dynamic type of an object,
  • a type that is a signed or unsigned type corresponding to the standard version of the dynamic type cv object,
  • the aggregate or type of association, which includes one of the above types among its members (including, recursively, a member of a subordinate or contained association),
  • a type that is (possibly cv-qualified) a base class type of a dynamic object type,
  • a char or unsigned char type.

In your case *derived is either the l-value of the dynamic type of the object, or the type that is the type of the base class of the dynamic type of the object. *base has a type that is the type of the base class of the dynamic type of an object.

Therefore, you are allowed to access the object through derived and base , which makes the two pointers an alias.

+5
source share

All Articles