C++ 警告:枚举值未在开关 [-Wswitch] 中处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37254496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:41:02  来源:igfitidea点击:

c++ warning: enumeration value not handled in switch [-Wswitch]

c++switch-statementwarningscompiler-warnings

提问by bluszcz

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.

这对我来说不是问题,因为我不需要处理所有类型的事件。

Adding

添加

default:
    break;

to my code removes the warnings, however is it a best way to solve this issue?

to my code 删除了警告,但是这是解决此问题的最佳方法吗?

回答by Andrew

Be explicit

明确

It depends on what you are trying to achieve. The governing rule is

这取决于您要实现的目标。统治规则是

It is better to be explicit.

最好是明确的。

Omitting the cases simply makes it look like you forgot some. Being explicit assures subsequent readers of your code that you intendedto do nothing for certain events.

省略案例只会让您看起来好像忘记了一些。作为明确的保证代码的后续读者,你打算做什么都不对某些事件。

In light of that, you have a couple of options:

有鉴于此,您有几个选择:

Option 1 - add the default

选项 1 - 添加默认值

default:
  break;

This suppresses the warning, and makes it clear that you don't intend to handle the other event types here.

这会抑制警告,并明确表示您不打算在此处处理其他事件类型。

Option 2 - list each value

选项 2 - 列出每个值

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 switchis 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.

列出每个事件类型,后跟一个break. 这也是明确的,并且有额外的好处,如果您添加事件类型,编译器将再次警告您您的事件类型switch不完整。当您有许多 switch 语句时,这可能很有价值,当添加枚举值时,其中一些需要修改以执行新的操作。

What about a series of if statements?

一系列的 if 语句呢?

I would not recommend using a series of ifstatements here. A switchis clearer, reduces the amount of typing, and (as you've seen) can produce better compiler warnings for cases you omitted.

我不建议if在这里使用一系列语句。Aswitch更清晰,减少了输入量,并且(如您所见)可以为您省略的情况生成更好的编译器警告。