bug report
Martin von Loewis
martin@mira.isdn.cs.tu-berlin.de
Fri Nov 20 15:28:00 GMT 1998
> It's not a different class per se, it's a derived class. Also, the code
> works on CC (SUN's) 4.2 as well as Portland Groups compiler.
This is both irrelevant. To quote from ISO C++, 5.2.10
[expr.reinterpret.cast]/2:
>> The mapping performed by reinterpret_cast is
>> implementationÃÂdefined. [Note: it might, or might not, produce a
>> representation different from the original value. ]
More detail on pointer-to-pointer conversions is in paragraph 7:
>> A pointer to an object can be explicitly converted to a pointer to
>> an object of different type.65) 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.
So we could give you any value as Derived*, as long as we restore the
original Base* when you reinterpret_cast<Base*>.
Your program relies on unspecified behaviour. There is nothing wrong
with CC and PCG doing what you expect, but this doesn't mean any other
compiler in the world should.
If you really need virtual base classes and conversion to derived
classes, use polymorphic types instead.
Regards,
Martin
More information about the Gcc-bugs
mailing list