So, the bit that looks as though it should work, but isn't portable is:
Code:
((long)"Well, hello there." & 0x3)
The reason being that "long" does not necessarily map to the size of a pointer - it does on most variants of unix, but the standard only requires it to be at
least 32-bits (and in MS C++, it remains so, even on 64-bit platforms). This is true even though it is and-ed with 0x3, which means that there is functionally no difference, regardless of it being 64-bit or 32-bit. This bit of code actually compiles on all platforms I've seen, and is a common source of compatibility errors from many experienced unix programmers. Incidentally, if "long" is replaced with an "int" on unix, it will require -fpermissive to compile at all (with a warning).
The really interesting bit, is the remaining code, which is now essentially:
Where X is an integer with the value 6 plus some integer between 0 and 3 inclusive. What is being taken advantage of over here is that (this is the bit I found surprising) in a piece of code like this, the following assignments are legal and equivalent:
Code:
int m[5];
...
int x = m[3];
// Is identical to:
int x = 3[m];
The reason for this is that m[3] = *(m+3) = *(3+m) = 3[m]. In the case of a string literal, which is just an array of characters, this means that 6["Hello World"], will give you 'W', and &6["Hello World"], will give you the pointer to 'W' in the string, so that cout would print out "World".
Because of the offset from the and-ed pointer, which incidentally, can be anything from 0-3 inclusive, since char alignment only has to align to its element size, the output will be 'World', 'orld', 'rld' or 'ld'.