c++ 警告:枚举值未在开关 [-Wswitch] 中处理
我正在尝试编译以下代码而没有警告:
I am trying to compile following code without warnings:
while (window.pollEvent(event))
{
switch (event.type) {
case sf::Event::Closed:
window.close(); break;
case sf::Event::KeyPressed:
if(event.key.code == sf::Keyboard::Escape )
window.close();
if( sf::Keyboard::isKeyPressed( sf::Keyboard::Space ) )
particleSystem.fuel( 200/* * window.getFrameTime() */);
if( sf::Keyboard::isKeyPressed( sf::Keyboard::A ) )
particleSystem.setPosition( --xpos, ypos );
if( sf::Keyboard::isKeyPressed( sf::Keyboard::D ) )
particleSystem.setPosition( ++xpos, ypos );
if( sf::Keyboard::isKeyPressed( sf::Keyboard::W ) )
particleSystem.setPosition( xpos, --ypos );
if( sf::Keyboard::isKeyPressed( sf::Keyboard::S ) )
particleSystem.setPosition( xpos, ++ypos );
if( sf::Keyboard::isKeyPressed( sf::Keyboard::Left ) )
particleSystem.setGravity( --xgrv * 0.1f, ygrv * 0.1f);
if( sf::Keyboard::isKeyPressed( sf::Keyboard::Right ) )
particleSystem.setGravity( ++xgrv * 0.1f, ygrv * 0.1f );
if( sf::Keyboard::isKeyPressed( sf::Keyboard::Up ) )
particleSystem.setGravity( xgrv * 0.1f, --ygrv * 0.1f );
if( sf::Keyboard::isKeyPressed( sf::Keyboard::Down ) )
particleSystem.setGravity( xgrv * 0.1f, ++ygrv * 0.1f );
if( sf::Keyboard::isKeyPressed( sf::Keyboard::G ) )
particleSystem.setGravity( 0.0f, 0.0f );
if( sf::Keyboard::isKeyPressed( sf::Keyboard::P ) )
particleSystem.setPosition( 320.0f, 240.0f );
break;
}
但是,我收到了很多警告:
however, I am getting a lot of warnings:
/home/bluszcz/private/repo/deerportal/game.cpp:444: warning: enumeration value 'LostFocus' not handled in switch [-Wswitch]
这对我来说不是问题,因为我不需要处理所有类型的事件.
Which in my it is not an issue, since I am don't need handling all types of the events.
添加
default:
break;
我的代码删除了警告,但是这是解决此问题的最佳方法吗?
to my code removes the warnings, however is it a best way to solve this issue?
推荐答案
要明确
这取决于您要达到的目标.管理规则是
Be explicit
It depends on what you are trying to achieve. The governing rule is
最好是明确的.
省略案例只会让您看起来好像忘记了一些案例.明确表明您的代码的后续读者打算对某些事件不做任何事情.
Omitting the cases simply makes it look like you forgot some. Being explicit assures subsequent readers of your code that you intended to do nothing for certain events.
鉴于此,您有两种选择:
In light of that, you have a couple of options:
default:
break;
这会抑制警告,并清楚表明您不打算在此处处理其他事件类型.
This suppresses the warning, and makes it clear that you don't intend to handle the other event types here.
列出每个事件类型,后跟 break
.这也是明确的,并且有额外的好处,如果您添加事件类型,编译器将再次警告您您的 switch
不完整.当您有许多 switch 语句时,这可能很有价值,其中一些需要在添加枚举值时进行修改以执行新的操作.
List each event type, followed by a break
.
This is also explicit, and has the added bonus that, should you ever add an event type, the compiler will once again warn you that your switch
is incomplete. This can be valuable when you have many switch statements, some of which need to be modified to do something new when an enum value is added.
我不建议在这里使用一系列 if
语句.switch
更清晰,减少了输入量,并且(如您所见)可以为您省略的情况产生更好的编译器警告.
I would not recommend using a series of if
statements here. A switch
is clearer, reduces the amount of typing, and (as you've seen) can produce better compiler warnings for cases you omitted.
相关文章