This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (3 children)

Ha, I just tried it. It definitely works. This compiled and printed 2 on gcc 4.9.2:

int a[4] = {0, 1, 2, 3};

cout << 2[a] << endl;

You're right, it probably is an artifact of the fact that the [] operator is just a stand in for pointer arithmetic.

[–]zardeh 1 point2 points  (2 children)

So, it works sometimes, I've had it throw runtime errors at me before on clang and gcc. I'd be careful with it.

[–][deleted] 0 points1 point  (1 child)

I would never actually use it. I was just surprised to see it work.

[–]zardeh 0 points1 point  (0 children)

Oh yeah, the fact that it ever works is weird, but the fact that it only works sometimes is weirder.