为什么C4062 Visual C++默认情况下警告关闭?

发布于 2024-12-12 03:19:03 字数 400 浏览 0 评论 0原文

根据 MSDN,当

  • 使用枚举 时,Visual C++ 会发出 C4062 警告switch 中,并且
  • 该枚举的至少一个元素没有标签,并且
  • switch 中没有 default: 标签

现在对我来说这种情况当然值得警告——很有可能该元素处理不当。如果不需要对某些元素执行任何操作 - 开发人员可以提供空的 casedefault

默认情况下关闭此警告的原因可能是什么?

According to MSDN, Visual C++ can emit C4062 warning when

  • and enumeration is used in switch and
  • there's no label for at least one element of that enumeration and
  • there's no default: label in the switch

Now to me such situation certainly deserves a warning - there's a good chance that the element in question is mishandled. If nothing has to be done for some elements - the developer can provide either an empty case or default.

What could be the reason why this warning is off by default?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

旧情勿念 2024-12-19 03:19:03

有些人(包括我自己)喜欢在构建时看到“0 警告”。如果您只不处理少数情况,那么添加一个空案例可能没问题,但如果您正在使用一个输入库,该库为您提供一个显示哪个键被按下的枚举,那么您真的想要 200 多个空案例吗?您不处理的密钥?

当然,您可能会说只添加一个空的默认情况,但是:

  • 在上述情况下它并没有真正的语义意义
  • 现在您只是邀请 C4061 (这一切在哪里结束?)

所以如果我默认收到这些警告,它真的会让我的强迫症感到紧张

There are certain folk (in which I include myself) who like to see '0 Warnings' whenever they build. Adding an empty case might be OK if you're only not handling a few cases, but if you're working say with an input library which gives you an enum showing which key is down, do you really want 200+ empty cases for the keys you don't process?

Of course, you might say just add an empty default case, but:

  • It doesn't really make semantic sense in the above case
  • Now you're just inviting C4061 (where does it all end?)

So it would really set my OCD on edge if I got these warnings by default

远山浅 2024-12-19 03:19:03

如果默认情况下此警告处于打开状态,则在不编辑现有(已工作)代码的情况下,enum 可能无法始终保持可扩展性。例如

// (c) 2009
// header.h
enum E { a, b, c };
...
// legacy.cpp
switch(e)
{
case a:  ...
case b:  ...
case c:  ...
}

,假设 2 年后开发人员仅编辑 header.h。实现文件经过充分测试且未更改 (legacy.cpp)

// (c) 2011
// header.h
enum E { a, b, c, d, e };  // added 'd','e'

由于强制警告,legacy.cpp 可能会在以下位置出现大量警告: 和 e 未处理,这可能是不可取的。

If this warning is ON by default, then enums may not remain extendable always, without editing the existing (already working) code. e.g.

// (c) 2009
// header.h
enum E { a, b, c };
...
// legacy.cpp
switch(e)
{
case a:  ...
case b:  ...
case c:  ...
}

Suppose, after 2 years developer edits just the header.h. The implementation file is well tested and not changed (legacy.cpp)

// (c) 2011
// header.h
enum E { a, b, c, d, e };  // added 'd','e'

Due to the mandatory warning, the legacy.cpp may get flooded with warnings at places where d and e not handled, which may not be desirable.

裸钻 2024-12-19 03:19:03

这些警告可能是根据具体情况启用的。尝试使用 /Wall 编译 C++ 项目会从 Microsoft 自己的标头中产生数千个警告。

现在,您正在尝试调试一大段代码,您怀疑某处缺少 case 语句(或者您想排除这个假设)。然后启用 C4062。

如果你看得仔细的话,所有这些禁用的警告都是非常迂腐的,但它们可以用来追踪不明显的错误。您确实要启用 C4264 吗?

Those warnings are probably meant to be enabled on a case by case basis. Trying to compile a C++ project with /Wall yields thousands of warnings from Microsoft's own headers.

Now, you are trying to debug a large piece of code where you suspect that there is a case statement missing somewhere (or you want to rule out this hypothesis). You then enable C4062.

If you look well, all these disabled warnings are highly pedantic but have their use to track down obscure bugs. Do you really want to enable C4264 ?

猛虎独行 2024-12-19 03:19:03

人们出于不同的原因使用枚举。

许多其他人(显然包括微软)会以较少审查的意图使用它们。它们只是一个命名值,它们的分组并不是很有趣。在防守方面,他们有很多基础。

有一些边缘情况,比如 GuyCook 的例子,没有人会因为收到警告而感到高兴。他说得对,没有人愿意看那些垃圾。在这种情况下,我已将处理代码放置在自己的 cpp 文件中,因此在不更改标头的情况下不会重新编译它。我希望有一种更方便的方法来解决这个问题,但我认为不存在。

我很欣赏语言(C#/Java?),因为它们能够通过注释忽略警告的特定实例。

事实上,您对它的省略感到困惑,这意味着您可能正在以设计中最有意义的方式使用它们。我个人认为枚举在耦合和内聚方面应该受到与类相同的审查。如果有人更改了枚举,则应审查代码。如果有人更改了您继承的类的接口,您会想知道这一点,不是吗?

不过,他们不必以这种方式使用它们。枚举只是一种工具,有些人更喜欢将它用作东西的同义词。 :)

People use enums for different reasons.

Lots of other people (Microsoft included, obviously) will use them with much less scrutinized intention. They are just a named value, and their groupings aren't very interesting. In their defense, they have a lot of ground to cover.

There are some edge cases like GuyCook's example that nobody will be happy with getting a warning about. He's right that nobody wants to look at that crap. In cases like that I've placed handling code in its own cpp file so it wasn't recompiled without a change to the header. I wish there were a more convenient way to solve that problem, but I don't think there is.

I admire languages(C#/Java?) for their ability to ignore specific instances of a warning with annotations.

The fact that you are befuddled with its omission means you are probably using them in the way that they are the most meaningful in a design. I personally think enums should be given the same scrutiny as classes in regards to coupling and cohesion. If someone changes an enum, the code should be reviewed. If someone changed an interface on a class you inherited from, you'd want to know about that, wouldn't you?

They don't have to use them in that way, though. An enum is just a tool, some prefer to use it as a synonym for stuff. :)

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