From e9f95eb4d6e31d2010f55f248713a7072e5597ff Mon Sep 17 00:00:00 2001 From: David Capello Date: Wed, 23 Sep 2015 16:55:18 -0300 Subject: [PATCH] Complete ES.74 section --- CppCoreGuidelines.md | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index f28366f..6484dd3 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -7212,14 +7212,26 @@ This will copy each elements of `vs` into `s`. Better ### ES.74: Prefer to declare a loop variable in the initializer part of as `for`-statement -**Reason**: ??? +**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 + // ... + } -**Enforcement**: ??? +**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**: Warn when a variable modified inside the `for`-statement is declared outside the loop and not being used outside the loop. ### ES.75: Avoid `do`-statements