![]() |
|
Rule 0.1.1 Example f4 - Printable Version +- MISRA Discussion Forums (https://forum.misra.org.uk) +-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18) +--- Forum: MISRA C++:2023 guidelines (https://forum.misra.org.uk/forumdisplay.php?fid=188) +---- Forum: 4.0 Language independent issues (https://forum.misra.org.uk/forumdisplay.php?fid=189) +---- Thread: Rule 0.1.1 Example f4 (/showthread.php?tid=1787) |
Rule 0.1.1 Example f4 - cgpzs - 30-09-2026 Hi, In MISRA C++:2023 TC1 Rule 0.1.1, the comment about example f4() says: As j is only incremented, no value is overwritten without first being read. Additionally, all iterations of the loop other than the first observe j after it has been incremented, meaning the write in the increment is necessary. This is not technically correct: the *last* iteration of the loop will *not* observe the value of j after it has been incremented. Do you consider that the last increment of "j" violates Rule 0.1.1? What about this? Code: template <int n>The above code never uses the incremented value of "j" for a template instantiation with "n = 1". Is that a violation of Rule 0.1.1? I see now that this exact use case was explicitly allowed in the original MISRA C++:2023 document, but removed in the TC1. Code: Within a loop, a value may be written to an object with the intent that it will be observed during theRE: Rule 0.1.1 Example f4 - misra cpp - 02-10-2026 Technically you are right, instantiating the template with n == 1 does lead to a violation for j++. However, we'd say that this code is within the spirit of the rule and it's likely to be too complex and confusing to add exceptions to the rule (and potentially many more) to address specific edge cases. If this causes a problem with an unexpected violation from a tool, then a deviation is an acceptable work-around (and better that a specialisation for n==1) RE: Rule 0.1.1 Example f4 - misra cpp - 06-10-2026 We didn't answer your first question: "Do you consider that the last increment of "j" violates Rule 0.1.1?" The last increment to 'j' does not violate the rule. (Except when n==0) the statement performing the write has one other occurance in the loop which is read, and then observed before being overwritten. RE: Rule 0.1.1 Example f4 - cgpzs - 07-10-2026 Thank you for the quick clarification! |