09-02-2023, 05:36 PM
I am looking for some clarification on the first non-compliant example for Rule 6-5-2, and the overall definition of a loop-counter, which as part of its definition states it must be,
Where relational operator is defined in Rule 4-5-2, 4-5-3, and §5.9 as <, >, <=, and >=.
In the non-compliant example
The variable i is clearly considered a loop-counter because the subject of Rule 6-5-2 is a loop-counter. I am confused as to how could i could be considered a loop-counter though, because it is not an operand to a relational operator in the for loop condition.
Moreover, if a loop-counter is defined as necessarily being an operand to a relational operator, then the point of Rule 6-5-2 is called into question. The only thing it could be applied to is when a variable is used as an operand to a relational operator (making it a loop-counter) and another expression in the conditions like,
Which is helpful for some bad behavior, but will clearly miss some fairly obvious bad behavior, like the original non-compliant example.
Tangentially related the definition of loop-counter contains a note explicitly saying that iterators are also valid as a loop-counter. However, this note would be odd, since typically when using STL iterators in loops equality operators are used and not relational operators. So Rule 6-5-1 would flag the following as non-compliant, even though it is a common idiom.
Quote:an operand to a relational operator in condition
Where relational operator is defined in Rule 4-5-2, 4-5-3, and §5.9 as <, >, <=, and >=.
In the non-compliant example
Code:
for ( i = 1; i != 10; i += 2 ) // Non-compliant
The variable i is clearly considered a loop-counter because the subject of Rule 6-5-2 is a loop-counter. I am confused as to how could i could be considered a loop-counter though, because it is not an operand to a relational operator in the for loop condition.
Moreover, if a loop-counter is defined as necessarily being an operand to a relational operator, then the point of Rule 6-5-2 is called into question. The only thing it could be applied to is when a variable is used as an operand to a relational operator (making it a loop-counter) and another expression in the conditions like,
Code:
for ( i = 1; i != 10 && i > 0 ; i += 2 ) // Non-compliant
Which is helpful for some bad behavior, but will clearly miss some fairly obvious bad behavior, like the original non-compliant example.
Tangentially related the definition of loop-counter contains a note explicitly saying that iterators are also valid as a loop-counter. However, this note would be odd, since typically when using STL iterators in loops equality operators are used and not relational operators. So Rule 6-5-1 would flag the following as non-compliant, even though it is a common idiom.
Code:
for (auto it = container.begin(); it != container.end(); ++it )