Skip to content
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

Subscript expressions #44

Closed
pepsiman opened this issue Jul 14, 2018 · 4 comments
Closed

Subscript expressions #44

pepsiman opened this issue Jul 14, 2018 · 4 comments
Labels
enhancement New feature or request

Comments

@pepsiman
Copy link

https://en.cppreference.com/w/cpp/language/operator_member_access says
"The built-in subscript expression E1[E2] is exactly identical to the expression *(E1 + E2)"
It's a useful insight when E1 is not an array or pointer, but E2 is.

@andreasfertig andreasfertig added the enhancement New feature or request label Jul 15, 2018
@andreasfertig
Copy link
Owner

Hello pepsiman,

wonderful. That's one of this nice language features where I ask myself why it is there. I did a quick test and if that the transformation is easy at a first glance, all cases are more difficult. Once again I'm thinking about an option to show it ore leave it the way it is. Of course, a PR is welcome.

@languagelawyer
Copy link

The built-in subscript expression E1[E2] is exactly identical to the expression *(E1 + E2)

Not exactly since C++14.
https://timsong-cpp.github.io/cppwp/n4140/expr.sub : "except that in the case of an array operand, the result is an lvalue if that operand is an lvalue and an xvalue otherwise."

using int_arr_t = int[2];
int main()
{
    #define E1 int_arr_t{ 0, 2 }
    #define E2 1
    #if 0
    E1[E2] = 1; // ill-formed, assignment requires lvalue, not xvalue
    #else
    *((E1)+(E2)) = 1; // well-formed
    #endif
}

@andreasfertig
Copy link
Owner

@languagelawyer thanks for pointing that out. I'm unaware of that change.

While reading my former response I think I should apologize for my bad English. Let me try again.
I did a quick test whether the requested transformation is easy to implement, which at the first glance is the case. But a bigger amount of tests does not compile with that change. Apart from that, C++ Insights does not have options, which adds another layer of todo.
I still keep this issue open and hope to get to it in near the future.

@andreasfertig andreasfertig added WIP Work in progress and removed WIP Work in progress labels Mar 24, 2019
@andreasfertig andreasfertig mentioned this issue Mar 24, 2019
@andreasfertig
Copy link
Owner

Hello @pepsiman,

a patch for this issue is on its way. For more details please refer to #45.

andreasfertig pushed a commit that referenced this issue Mar 24, 2019
…ession.

Subscriptions `E1[E2]` can also be written in the form `*(E1 = E2)`.
This patch adds a command line option to select this transformation.
mergify bot added a commit that referenced this issue Mar 24, 2019
Fixed #44: Support alternative representation for a subscription expression.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants