为什么C4062 Visual C++默认情况下警告关闭?
根据 MSDN,当
- 使用枚举 时,Visual C++ 会发出 C4062 警告在
switch
中,并且 - 该枚举的至少一个元素没有标签,并且
switch
中没有default:
标签
现在对我来说这种情况当然值得警告——很有可能该元素处理不当。如果不需要对某些元素执行任何操作 - 开发人员可以提供空的 case
或 default
。
默认情况下关闭此警告的原因可能是什么?
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 theswitch
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有些人(包括我自己)喜欢在构建时看到“0 警告”。如果您只不处理少数情况,那么添加一个空案例可能没问题,但如果您正在使用一个输入库,该库为您提供一个显示哪个键被按下的枚举,那么您真的想要 200 多个空案例吗?您不处理的密钥?
当然,您可能会说只添加一个空的默认情况,但是:
所以如果我默认收到这些警告,它真的会让我的强迫症感到紧张
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:
So it would really set my OCD on edge if I got these warnings by default
如果默认情况下此警告处于打开状态,则在不编辑现有(已工作)代码的情况下,
enum
可能无法始终保持可扩展性。例如,假设 2 年后开发人员仅编辑
header.h
。实现文件经过充分测试且未更改 (legacy.cpp
)由于强制警告,
legacy.cpp
可能会在以下位置出现大量警告: 和e
未处理,这可能是不可取的。If this warning is ON by default, then
enum
s may not remain extendable always, without editing the existing (already working) code. e.g.Suppose, after 2 years developer edits just the
header.h
. The implementation file is well tested and not changed (legacy.cpp
)Due to the mandatory warning, the
legacy.cpp
may get flooded with warnings at places whered
ande
not handled, which may not be desirable.这些警告可能是根据具体情况启用的。尝试使用
/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 ?
人们出于不同的原因使用枚举。
许多其他人(显然包括微软)会以较少审查的意图使用它们。它们只是一个命名值,它们的分组并不是很有趣。在防守方面,他们有很多基础。
有一些边缘情况,比如 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. :)