From 34e636c2f413d4279f3af9a878ac1ffd60373a37 Mon Sep 17 00:00:00 2001 From: Ameen Ali Date: Tue, 22 Sep 2015 22:03:46 +0300 Subject: [PATCH 1/2] update at ES.74 Rule add (Reason) and (Example) at (Prefer to declare a loop variable in the initializer part of as `for`-statement) Rule --- CppCoreGuidelines.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 9794ac8..92efad6 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -7212,11 +7212,15 @@ 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**: 1) By creating variables inside loops, you ensure their scope is restricted to inside the loop. It cannot be referenced nor called outside of the loop. + 2) some dedicated optimization can be performed more efficiently by the compiler (most importantly register allocation), since it knows that the variable cannot be used outside of the loop. For example, no need to store the result for later re-use. **Example**: - ??? + for(int counter = 0; counter <= 10; counter++) + { + cout << counter << endl ; + } **Enforcement**: ??? From 8ebb29c2493f5cf73553c1b35c538f716b4f4b31 Mon Sep 17 00:00:00 2001 From: Andrew Pardoe Date: Thu, 24 Sep 2015 07:12:29 -0700 Subject: [PATCH 2/2] Merged commit for PR #86 and fixed formatting --- CppCoreGuidelines.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index a1fb00b..4c0ecb5 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -7212,15 +7212,18 @@ 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**: 1) By creating variables inside loops, you ensure their scope is restricted to inside the loop. It cannot be referenced nor called outside of the loop. - 2) some dedicated optimization can be performed more efficiently by the compiler (most importantly register allocation), since it knows that the variable cannot be used outside of the loop. For example, no need to store the result for later re-use. +**Reason**: 1) By creating variables inside loops, you ensure their scope is restricted to inside the loop. It + cannot be referenced nor called outside of the loop. + 2) some dedicated optimization can be performed more efficiently by the compiler (most importantly + register allocation), since it knows that the variable cannot be used outside of the loop. For + example, no need to store the result for later re-use. **Example**: - for(int counter = 0; counter <= 10; counter++) - { + for (int counter = 0; counter <= 10; counter++) + { cout << counter << endl ; - } + } **Enforcement**: ???