如何修复Misra C++规则0-1-4
以下代码违反了MISRA C ++规则0-1-4:
for (auto &a : b) {
... // The variable a is used only in the for condition.
}
规则:项目不包含仅使用一次的非挥发式POD变量。变量“ a”仅使用一次,即在初始化期间。
我尝试的内容:
for (const auto &a : b) {
... // The variable a is used only in the for condition.
}
但这不是解决方案。
有人知道如何解决吗?
The following code violates the MISRA C++ rule 0-1-4:
for (auto &a : b) {
... // The variable a is used only in the for condition.
}
Rule: A project shall not contain non-volatile POD variables having only one use. Variable 'a' is used only once, that is, during initialization.
What I tried:
for (const auto &a : b) {
... // The variable a is used only in the for condition.
}
But that was not the solution.
Does anyone have an idea how I can fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
las当前的C ++语法要求您在使用 loop的
的范围形式时声明一个变量:
for(auto&:b){,
尽管不允许,尽管它具有潜在的应用程序(例如计算容器中的元素数量)。
写作
或
在循环体中可能会根据类型而起作用。这会欺骗静态分析仪,您可以希望表达能够汇总出来。
Alas the current C++ grammar requires you to declare a variable when using the range-for form of the
for
loop:for (auto& : b) {
is not allowed, despite it having potential applications (such as computing the number of elements in a container).
Writing
or
in the loop body might work depending on the type. This would fool the static analyser and you can hope the expression gets compiled out.