为什么C4062默认情况下关闭Visual C ++警告? [英] Why is C4062 Visual C++ warning off by default?

查看:153
本文介绍了为什么C4062默认情况下关闭Visual C ++警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据MSDN,Visual C ++可以发出 C4062警告,当




  • 并且枚举用于 switch

  • 该枚举的至少一个元素没有标签,

  • 默认值: > switch



现在对我来说这种情况当然值得警告 - 问题处理不当。如果对于某些元素没有做任何事情 - 开发者可以提供一个空的 case default



这个警告默认关闭的原因是什么?

解决方案

如果默认情况下此警告为ON,则枚举可能不会始终保持可扩展,而无需编辑现有的(已经工作的)代码。例如

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

假设2年后开发者编辑只是 header.h 。实施文件经过良好测试,未更改( legacy.cpp

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

由于强制警告, legacy.cpp d e 处可能会出现警告处理,这可能是不可取的。


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?

解决方案

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.

这篇关于为什么C4062默认情况下关闭Visual C ++警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆