-
Notifications
You must be signed in to change notification settings - Fork 772
Standard omission pertaining to the subscript operator #1609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
What do you mean by "operator [] may also be global"? [over.sub] says:
|
Yes, I see now that operator [] cannot be global. Probably my second alternative would be a little more clear than what's currently in the standard, but I am not sure if that would be a discussion worth having. I am closing the issue. |
That suggestion would not have been an improvement IMO. "C array" is not a defined term. It only occurs once in the standard, in a footnote in [valarray.cons] (and that should probably be fixed). The subscript operator is also valid for pointers, not just arrays. |
Yes indeed the following code compiles:
int n = 0;
int * pn = &n;
pn[0] = 0[pn] = 1;
IMO it makes little sense to spend time on refining this note. I hope that at some point the 0[pn] syntax will become illegal, and the note will either be edited or remoted.
From: Jonathan Wakely [mailto:notifications@github.com]
Sent: Monday, April 10, 2017 5:14 AM
To: cplusplus/draft <draft@noreply.github.com>
Cc: levmin <lminkovsky@outlook.com>; State change <state_change@noreply.github.com>
Subject: Re: [cplusplus/draft] Standard omission pertaining to the subscript operator (#1609)
That suggestion would not have been an improvement IMO. "C array" is not a defined term. It only occurs once in the standard, in a footnote in [valarray.cons] (and that should probably be fixed). The subscript operator is also valid for pointers, not just arrays.
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub<#1609 (comment)>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AZ6UN95gga77LOJEYEVVQFvPcu4ZLjqVks5rufLLgaJpZM4M4Mqb>.
|
This is well out of scope for this editorial issue tracker. (If you'd like to pursue this, please consider starting on the std-proposals mailing list.) |
I found a minor omission in the latest version of C++ standard. http://eel.is/c++draft/dcl.array, note 6 say:
[ Note: Except where it has been declared for a class (http://eel.is/c++draft/over.sub), the subscript operator [] is interpreted in such a way that E1[E2] is identical to *((E1)+(E2)) (http://eel.is/c++draft/expr.sub). 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 ofE1. Therefore, despite its asymmetric appearance, subscripting is a commutative operation. — end note ]
In essence, this says that the following function:
should compile.
The reference to http://eel.is/c++draft/over.sub makes it clear that what is meant here is overloaded subscript operator within a class, not an operator with a class type as a parameter.
The issue here is that operator [] may also be global, and those would also be an "exception". Perhaps we should rewrite this note along the following lines:
[ Note: Except where it has been declared for a class (http://eel.is/c++draft/over.sub) or in a global or namespace scope, the subscript operator [] is interpreted in such a way that E1[E2] is identical to *((E1)+(E2)) (http://eel.is/c++draft/expr.sub). 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 ofE1. Therefore, despite its asymmetric appearance, subscripting is a commutative operation. — end note ]
Another alternative would be:
[ Note: For C arrays, the subscript operator [] is interpreted in such a way that E1[E2] is identical to *((E1)+(E2)) (http://eel.is/c++draft/expr.sub). 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 ofE1. Therefore, despite its asymmetric appearance, subscripting for C arrays is a commutative operation. — end note ]
The text was updated successfully, but these errors were encountered: