I wasn't saying the XOR trick is illegal in C++. (It does, however, rely on implementation defined behavior, so you likely couldn't use it in a strictly-conforming program.)
I was saying that it's illegal for the compiler to undo bad data structures, such as replacing your XORed pointers with proper prev/next pointers. If you have something like this:
struct ugly {
uint64_t prevnext;
};
Where 'prevnext' is the XORed pointer, the compiler isn't allowed to replace it with something more sane like:
struct ugly {
ugly *prev, *next;
};
...or even...
struct ugly {
uint32_t prevnext;
};
...if it figures out you picked an oversized integer for the storage.
TheRaven64 pointed out some cases where it can be legal for the compiler to rearrange / modify bad structures, but the gains tend to be minimal.
As I recall, the C++ standard does put some requirements on structure layout, as least for standard layout PODs:
* Minimum size of 1 for a structure so that each element of an array of empty structures has a distinct address
* Distinct addresses for all non-bitfield members
* Pointer to struct is convertable to pointer to first member and back
* Increasing addresses for members in order of specification
* Optional padding as required between members and at the end of the structure.
* Layout compatibility between identically declared standard-layout structs for their common initial sequence. (That's a mouthful!)
(There may be some others I'm forgetting... That list was off the top of my head.)
If a compiler wanted to rearrange a structure (say, to eliminate or minimize padding, or to eliminate unused members), it'd have to prove that the program didn't rely on any of the guarantees the standard offers that the compiler's otherwise violating. Violations of the standard by the optimizer are legal as long as you don't get caught. ;-)