C++ Logo

sg12

Advanced search

Re: [ub] type punning through congruent base class?

From: Jason Merrill <jason_at_[hidden]>
Date: Mon, 06 Jan 2014 13:22:24 -0500
On 01/06/2014 04:26 AM, Fabio Fracassi wrote:
> if it is not (legal): could we make it legal or would we run afoul of
> the aliasing rules?

The access is not allowed by the aliasing rules in 3.10. But it seems
that this would be:

struct B {
   int i;
};

struct D {
   B bmem;
   void foo() { /* access bmem.i */ }
};

B b;
reinterpret_cast<D&>(b).foo();

because B is a non-static data member of D, and 9.2/19 guarantees that
the address of D::bmem is the same as the address of the D object.

Jason

Received on 2014-01-06 20:23:35