如何修复Misra C++规则0-1-4

发布于 2025-01-28 13:34:55 字数 382 浏览 0 评论 0原文

以下代码违反了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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三寸金莲 2025-02-04 13:34:55

las当前的C ++语法要求您在使用 loop的的范围形式时声明一个变量:

for(auto&:b){,

尽管不允许,尽管它具有潜在的应用程序(例如计算容器中的元素数量)。

写作

a;

(void)a;

在循环体中可能会根据类型而起作用。这会欺骗静态分析仪,您可以希望表达能够汇总出来。

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

a;

or

(void)a;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文