RFR: 8145096: Undefined behaviour in HotSpot

John Rose john.r.rose at oracle.com
Fri Dec 11 18:39:19 UTC 2015


On Dec 11, 2015, at 10:17 AM, John Rose <john.r.rose at oracle.com> wrote:
> http://en.cppreference.com/w/cpp/language/reinterpret_cast

After reading the fine print, I see that integral-to-integral reinterpretations
are not part of the portfolio of reinterpret_cast.  But you can reinterpret
an lvalue of type unsigned int as a reference to type signed int, which
activates type aliasing rules that allow the intended conversion:

> • AliasedType is the (possibly cv-qualified) signed or unsigned variant of DynamicType

These rules (or similar rules elsewhere in the spec) may (or may not)
imply that the union trick, and/or memcpy trick, gets the desired result.

So, the lvalue cast makes the following macro definition plausible:

inline TYPE NAME (TYPE in1, TYPE in2) {                 \
 STATIC_ASSERT(sizeof(TYPE) == sizeof(UNSIGNED_TYPE)); \
 UNSIGNED_TYPE ures = static_cast<UNSIGNED_TYPE>(in1); \
 ures OP ## = static_cast<UNSIGNED_TYPE>(in2);         \
 return reinterpret_cast<TYPE&>(ures);                          \

More information about the hotspot-dev mailing list