Boost logo

Boost :

From: Sebastian Redl (sebastian.redl_at_[hidden])
Date: 2007-11-03 08:36:06


David Abrahams wrote:
> on Fri Nov 02 2007, "Boris Gubenko" <Boris.Gubenko-AT-hp.com> wrote:
>
>> You are, probably, referring to 5.2.10 - Reinterpret cast
>> [expr.reinterpret.cast], para 3:
>>
>> -3- The mapping performed by reinterpret_cast is implementation-defined.
>> [Note: it might, or might not, produce a representation different
>> from the original value. ]
>>
>> In my fix, reinterpret_cast performs a mapping from non-null pointer to
>> long to pointer to char. If reinterpret_cast cannot be used in this case
>> without invoking implementation-defined behavior, I'm not sure how it can
>> be used in a multiplatform code like Boost at all.
>>
>
> Very perceptive. There are almost no ways to use reinterpret_cast
> portably.
In particular, I've recently thought up this compiler behaviour -
obviously only enabled in debug mode and intentionally meant to break
programs that misuse reinterpret_cast, but, as far as I can tell,
perfectly compliant. It's simple: on every reinterpret_cast, flip the
most significant bit of the pointer. When you do the cast back to the
original type, you have your pointer back. But the intermediate value is
almost guaranteed to be invalid. (Think of the 2G barrier on typical
x86-32 systems, or the fact that x86-64 only uses 48 address bits.)
The tricky part is that you have to compare 0x00000000 and 0x70000000
equal, or not flip the bit for null, in order to keep the null
preserving that is required of reinterpret_cast.

Sebastian Redl


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk