mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2025-12-17 12:44:42 +03:00
@@ -10212,7 +10212,10 @@ In this case, it might be a good idea to factor out the read:
|
||||
|
||||
##### Reason
|
||||
|
||||
Readability. Minimize resource retention.
|
||||
Readability.
|
||||
Limit the loop variable visibility to the scope of the loop.
|
||||
Avoid using the loop variable for other purposes after the loop.
|
||||
Minimize resource retention.
|
||||
|
||||
##### Example
|
||||
|
||||
@@ -10233,11 +10236,24 @@ Readability. Minimize resource retention.
|
||||
}
|
||||
}
|
||||
|
||||
##### Example, don't
|
||||
|
||||
int j; // BAD: j is visible outside the loop
|
||||
for (j = 0; j < 100; ++j) {
|
||||
// ...
|
||||
}
|
||||
// j is still visible here and isn't needed
|
||||
|
||||
**See also**: [Don't use a variable for two unrelated purposes](#Res-recycle)
|
||||
|
||||
##### Enforcement
|
||||
|
||||
* Flag loop variables declared before the loop and not used after the loop
|
||||
* Warn when a variable modified inside the `for`-statement is declared outside the loop and not being used outside the loop.
|
||||
* (hard) Flag loop variables declared before the loop and used after the loop for an unrelated purpose.
|
||||
|
||||
**Discussion**: Scoping the loop variable to the loop body also helps code optimizers greatly. Recognizing that the induction variable
|
||||
is only accessible in the loop body unblocks optimizations such as hoisting, strength reduction, loop-invariant code motion, etc.
|
||||
|
||||
##### C++17 and C++20 example
|
||||
|
||||
Note: C++17 and C++20 also add `if`, `switch`, and range-`for` initializer statements. These require C++17 and C++20 support.
|
||||
@@ -10255,8 +10271,6 @@ Note: C++17 and C++20 also add `if`, `switch`, and range-`for` initializer state
|
||||
* Flag selection/loop variables declared before the body and not used after the body
|
||||
* (hard) Flag selection/loop variables declared before the body and used after the body for an unrelated purpose.
|
||||
|
||||
|
||||
|
||||
### <a name="Res-name-length"></a>ES.7: Keep common and local names short, and keep uncommon and non-local names longer
|
||||
|
||||
##### Reason
|
||||
@@ -12718,39 +12732,7 @@ Flag actions in `for`-initializers and `for`-increments that do not relate to th
|
||||
|
||||
### <a name="Res-for-init"></a>ES.74: Prefer to declare a loop variable in the initializer part of a `for`-statement
|
||||
|
||||
##### Reason
|
||||
|
||||
Limit the loop variable visibility to the scope of the loop.
|
||||
Avoid using the loop variable for other purposes after the loop.
|
||||
|
||||
##### Example
|
||||
|
||||
for (int i = 0; i < 100; ++i) { // GOOD: i var is visible only inside the loop
|
||||
// ...
|
||||
}
|
||||
|
||||
##### Example, don't
|
||||
|
||||
int j; // BAD: j is visible outside the loop
|
||||
for (j = 0; j < 100; ++j) {
|
||||
// ...
|
||||
}
|
||||
// j is still visible here and isn't needed
|
||||
|
||||
**See also**: [Don't use a variable for two unrelated purposes](#Res-recycle)
|
||||
|
||||
##### Example
|
||||
|
||||
for (string s; cin >> s; ) {
|
||||
cout << s << '\n';
|
||||
}
|
||||
|
||||
##### Enforcement
|
||||
|
||||
Warn when a variable modified inside the `for`-statement is declared outside the loop and not being used outside the loop.
|
||||
|
||||
**Discussion**: Scoping the loop variable to the loop body also helps code optimizers greatly. Recognizing that the induction variable
|
||||
is only accessible in the loop body unblocks optimizations such as hoisting, strength reduction, loop-invariant code motion, etc.
|
||||
See [ES.6](#Res-cond)
|
||||
|
||||
### <a name="Res-do"></a>ES.75: Avoid `do`-statements
|
||||
|
||||
|
||||
Reference in New Issue
Block a user