Use of array[i] vs. i[array] subscripting

115 views
Skip to first unread message

kylekn...@gmail.com

unread,
Dec 4, 2018, 9:44:30 AM12/4/18
to ISO C++ Standard - Discussion
What are the use cases for preferring the i[array] syntax over the array[i] syntax?  In other words, consider the following code:

int i[2] = {0, 1};
int j[2][2] = {{2, 3}, {4, 5}};

assert(i[0] == 0);
assert(i[0] == 0[i]);

assert(j[0][0] == 2);
assert(j[0][0] == 0[0[j]]);

Would there be any benefit to deprecating the reversed syntax (i.e. i[array])?  Since there is talk about eventually supporting multi-index subscripting:

assert(j[2][2] == j[2,2]);

would the reversed subscripting cause problems?

Thanks for your input,
Kyle

Myriachan

unread,
Dec 5, 2018, 9:06:07 PM12/5/18
to ISO C++ Standard - Discussion
On Tuesday, December 4, 2018 at 6:44:30 AM UTC-8, kylekn...@gmail.com wrote:
What are the use cases for preferring the i[array] syntax over the array[i] syntax?  In other words, consider the following code:

It's mainly because the actual definition of x[y] in the absence of overloading is *(x + y), which derives from C history.

I wish non-member operator [] were supported, because right now, smart pointers can't use the 0[smartArray] syntax.
Reply all
Reply to author
Forward
0 new messages