Expressions and statements
ES.71
Prefer a range-`for`-statement to a `for`-statement when there is a choice
Reason
Readability. Error prevention. Efficiency.
Example
for (gsl::index i = 0; i < v.size(); ++i) // bad
cout << v[i] << '\n';
for (auto p = v.begin(); p != v.end(); ++p) // bad
cout << *p << '\n';
for (auto& x : v) // OK
cout << x << '\n';
for (gsl::index i = 1; i < v.size(); ++i) // touches two elements: can't be a range-for
cout << v[i] + v[i - 1] << '\n';
for (gsl::index i = 0; i < v.size(); ++i) // possible side effect: can't be a range-for
cout << f(v, &v[i]) << '\n';
for (gsl::index i = 0; i < v.size(); ++i) { // body messes with loop variable: can't be a range-for
if (i % 2 != 0)
cout << v[i] << '\n'; // output odd elements
}
A human or a good static analyzer might determine that there really isn't a side effect on v in f(v, &v[i]) so that the loop can be rewritten.
"Messing with the loop variable" in the body of a loop is typically best avoided.
Note
Don't use expensive copies of the loop variable of a range-for loop:
for (string s : vs) // ...
This will copy each element of vs into s. Better:
for (string& s : vs) // ...
Better still, if the loop variable isn't modified or copied:
for (const string& s : vs) // ...
Enforcement
Look at loops, if a traditional loop just looks at each element of a sequence, and there are no side effects on what it does with the elements, rewrite the loop to a ranged-for loop.