-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Guideline for how to declare variables in a range-based for loop #2115
Comments
Consider the following issues with using by-value range-for variables: // Scenario (1): the author does modify 'x' within the loop, so they intentionally make 'x' mutable.
// This conforms with CppCoreGuidelines.
for (int x : container) {
x = ...; // POSSIBLE BUG, did they mean to modify the 'x' within the container?
int y = x;
y = ...; // OK, intent is clear :)
// It is obvious that the author meant to modify a local copy.
}
// Conclusion: the author should have used 'const int' or 'const int&' as a loop variable,
// and a mutable variable in the body. // Scenario (2): the author takes the address of a local variable in the loop.
for (const int x : container) {
use_address(&x); // POSSIBLE BUG, did they mean to take the address of the 'x' within the container?
const int y = x;
use_address(&y); // OK, intent is clear :)
// It is obvious that the author meant to take the address of a local variable.
}
// Conclusion: the author should have used 'const int&' as a loop variable,
// and a 'const' variable in the body. In both of these examples, if the author had consistently used |
So i just browsed through the guidelines because I thought I remembered reading some pipeline specifically about Most of those examples (e.g. P.3, ES.42, ES.55 and ES.71) use either |
There are also people who default to |
related #1011 |
I was looking through the guidelines and noticed that there wasn't much advice in general about using auto. The closest one is ES.11 which says to use auto but not how. Maybe that rule can be expanded with special cases mentioned for I think it will be a difficult rule to write because there are many different coding styles and corner cases. To be consistent maybe Using |
A question which comes up every now and then is what type of loop variable you should use for range-based for, particularly when no mutation takes place.
I think we should recommend to always declare the loop variable as a reference, never as a value, and here's why:
T&
variable in the loop, you are already forced to follow it by the language.const T x
is only one character away fromconst T &x
, so it costs very little effort to follow this rule.std::ranges::iota_view::iterator
, where theirreference
is actually a value, this method is robust becauseconst&
allows for temporary materialization.In essence, I don't see any benefit to non-reference loop variables, and the alternative is consistent, easy, correct by default, and already recommended in part.
The text was updated successfully, but these errors were encountered: