-
Notifications
You must be signed in to change notification settings - Fork 251
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
[BUG] Variables in "block scope parameter" declarations should not default to in
(const)
#1000
Comments
Thanks! I hear you... I've switched the default for statement scope parameters between The most recent switch to the See also the terse commit comments on that comment. But I've continued to hit this myself, where I have to be reminded to write I'm not sure how to attach a poll to this issue... I'd love to get a quick poll of how many people think |
GitHub Discussions support polls but I think you have to enable them. |
Ah, I finally found it -- enabled, thanks. |
Thanks, everyone! Based on the results of #1000 I'll close this as "status quo" for now, and we can always reopen it again in the future if we get new information. |
Describe the bug
(Apologies if this should be a suggestion rather than a bug!)
I want to write a loop and also use a counter, but I want the counter variable to be scoped with the loop, and not accessible outside. When I use the "block scope parameter" feature (not sure if that's the correct name!) the variable defaults to
in
and is thereforeconst
, so I can't update the counter inside the loop.To Reproduce
Run cppfront on this code:
See Godbolt.
The same applies when using the
next
syntax:The relevant code lowers to:
and causes a C++ compiler error:
cannot assign to variable 'i' with const-qualified type 'cpp2::in<int>' (aka 'const int')
My first thought was to try
inout
since I wanted to mutatei
, but that doesn't work (see below).I get the desired behaviour by writing:
The 3 currently supported options are:
cpp2::in<int> i{0};
int i{0};
int& i{0};
Desired behaviour
There's a lot to like about unifying functions and local/block scope parameters, but I think variables declared in the "block scope parameter" list should have different defaults to function parameters.
My reasoning:
const
then the current block scope parameter behaviour would be consistent with them, but as it is there's a difference.copy
is not intuitive and also reads awkwardly.The text was updated successfully, but these errors were encountered: