Re:C++17 creates a practical use of the backward array index...
by jbverschoor on Monday April 03, 2023 @04:22PM
(#35430721)
I’ve never seen that before. Luckily
Re:C++17 creates a practical use of the backward array index...
by olliej on Monday April 03, 2023 @04:25PM
(#35430752)
He was also a tutor/TA and I think mostly used it to explain how pointers vs. indexing worked. Or possibly to traumatize students :D
Re:C++17 creates a practical use of the backward array index...
by int_19h on Monday April 03, 2023 @07:17PM
(#35432724)
It is extremely rare even in ancient C code. The only reason why it exists in C at all is because rewriting a[i] as *(a+i) was inherited by C from B; the latter only has a single machine word type for everything, so there was no way to define it such that a[i] would be legal but i[a] would be illegal in B.
Re:C++17 creates a practical use of the backward array index...
by HelloNurse on Tuesday April 04, 2023 @04:47AM
(#35436852)
In the competing array indexing syntax
*(a+b)
there is the same evaluation order issue, and they should have the same behaviour: is a evaluated before b or the compiler is allowed to choose an order? Clearly it depends on the rules for a+b
which are more important than the special case of array indexing.
The C++ change that makes this matter is that apparently pre c++17 doesn't enforce sequencing such that expression1[expression2] doesn't require expression1 be evaluated before expression2. C++17 does actually fix the sequencing to be left to right, so now expression1[expression2] will always evaluate as expression1;expression2 and expression2[expression1] will always evaluated as expression2;expression1 without depending on UB.