This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Linux and aliasing?
> [Could someone tell me if reinterpret_cast does the right thing with
> aliases please?]
No, it won't. The C++ standard says
>> A pointer to an object can be explicitly converted to a pointer to
>> an object of different type. Except that converting an rvalue of
>> type "pointer to T1" to the type "pointer to T2" (where T1 and T2
>> are object types and where the alignment requirements of T2 are no
>> stricter than those of T1) and back to its original type yields the
>> original pointer value, the result of such a pointer conversion is
>> unspecified.
In g++, conversion to a different pointer type in reinterpret_cast
will always yield a pointer that has the same internal representation.
However, dereferencing such a pointer has undefined result. You must
not access an object through a pointer to a different type, period (1).
This is a very easy rule (despite Linus' saying that it is very
complicated), and it is the foundation for allowing type-based alias
analysis optimizations.
Of course, the compiler could provide the local-overriding mechanism
that Linus proposed. It currently does not do so, neither for plain
casts, nor for reinterpret_casts.
Regards,
Martin
(1) except if that different type is char.