0

Older compilers allowed this kind of "blasphemy"

int a[5];
4[a] = 3;

Simply because operator[] would allow const expression and those are equal:

*(4 + a) === *(a + 4)

Is that a defined behavior or an UB in C++11?

Edit: answer:

Except where it has been declared for a class (13.5.5), the subscript operator [] is interpreted in such a way that E1[E2] is identical to *((E1)+(E2)). Because of the conversion rules that apply to +, if E1 is an array and E2 an integer, then E1[E2] refers to the E2-th member of E1. Therefore, despite its asymmetric appearance, subscripting is a commutative operation.

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42
  • Note, however, that most containers do not support this kind of syntax. – Brian Bi Dec 21 '16 at 23:42
  • @Brian "Except where it has been declared for a class". Containers declare subscript usually. SO it isn't declared and can't b used or it works differently. That's obvious – Swift - Friday Pie Dec 22 '16 at 06:31

0 Answers0