Reading Rapidjson code I found some interesting optimization with "type punning".
// By using proper binary layout, retrieval of different integer types do not need conversions.
union Number {
#if RAPIDJSON_ENDIAN == RAPIDJSON_LITTLEENDIAN
struct I {
int i;
char padding[4];
}i;
struct U {
unsigned u;
char padding2[4];
}u;
#else
struct I {
char padding[4];
int i;
}i;
struct U {
char padding2[4];
unsigned u;
}u;
#endif
int64_t i64;
uint64_t u64;
double d;
}; // 8 bytes
It looks like only BE CPUs are affected by this optimization. How does this increases performance? I'd like to test but do not have BE machine.
Wikipedia says:
While not allowed by C++, such type punning code is allowed as "implementation-defined" by the C11 standard[15] and commonly used[16] in code interacting with hardware.[17]
So is it legal in C++? I believe in absolute most cases it works fine. But should I use it in new code?