This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: C++ aliasing rules
>>>>> "Dan" == Dan Nicolaescu <dann@godzilla.ICS.UCI.EDU> writes:
> mike stump <mrs@windriver.com> writes:
>> Consider:
>>
>> struct { char buf[sizeof (double)]; short i; } secondo, *second = &secondo;
>> struct { double other; short i; } firsto, *not_really_first = &secondo;
>>
>> and the question of whether or not first->i can ever alias second->i.
>>
>> I suspect the closest we can come would be to
>>
>> memcpy (&secondo, &firsto, sizeof (secondo));
>>
>> and then play with not_really_first->i and second->i.
>>
>> I think the answer is no, as no matter what you do, you need to
>> violate the notion of which struct type the object really was and you
>> cannot get at the short without going though the struct first, and in
>> the end, you cannot get through both struct types simultaneously.
> I like this answer. :-)
> Does anybody disagree with Mike's statement above (wrt C++) ? Jason?
I don't see how Mike's statement answers my question. Given
struct A { char buf[sizeof (double)]; short i; };
struct B { double other; short i; };
A a;
A* ap = &a;
B* bp = reinterpret_cast<B*>(ap);
referring to bp->i is accessing a.i through an lvalue of a class type (B)
which contains a member of a.i's type (short). The standard seems to say
that's OK.
Again, if instead we consider a.i to be an indivisible part of 'a' for
purposes of [basic.lval], then the access would be undefined. I would
probably support writing that into the standard. But I don't think that's
what it says now.
Jason