There are apparently existing implementations that optimize based on this. Consider:
struct A {
double x[4];
int n;
};
void g(double* p);
int f() {
A a { {}, 42 };
g(&a.x[1]);
return a.n; // optimized to return 42;
// valid only if you can't validly obtain &a.n from &a.x[1]
}
Given p = &a.x[1];
, g
might attempt to obtain access to a.n
by reinterpret_cast<A*>(reinterpret_cast<double(*)[4]>(p - 1))->n
. If the inner cast successfully yielded a pointer to a.x
, then the outer cast will yield a pointer to a
, giving the class member access defined behavior and thus outlawing the optimization.