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

39,811

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 intended to do nothing for certain events.

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

Option 1 - add the default

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

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.

What about a series of if statements?

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.

Share:
39,811

Related videos on Youtube

bluszcz
Author by

bluszcz

Python / HTTP / XMPP / Linux / C / JS geek. System architect, web developer, vegan,

Updated on January 30, 2020

Comments

  • bluszcz
    bluszcz over 4 years

    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?

    • Rakete1111
      Rakete1111 about 8 years
      I think default is still the best way, you can still handle every event separately if you want to.
    • NathanOliver
      NathanOliver about 8 years
      You should almost always have a default case in case there is no match.
    • SergeyA
      SergeyA about 8 years
      I personally believe this warning (together with reordering of member initialization in constructor) is one of the least useful in gcc. I had tons of cases where not all enum values had a meaningful case switches, and doing an empty default just to pacify the compiler is meaningless syntax noise. That's why this warning is the one I am OK with suppressing.
    • Jesper Juhl
      Jesper Juhl about 8 years
      @NathanOliver I disagree. Sure, there are cases where a default: case is appropriate. But in the general case you should avoid it and handle all values explicitly so that the compiler can warn you when you forgot to handle a case.
  • bluszcz
    bluszcz about 8 years
    Nice argumentation of Option 2, however in my simply case where I am interested only of particular events handling, I think Option 1 is my case. Thank you.
  • Colby Cox
    Colby Cox about 7 years
    Option 1 did the job for me!
  • Dino
    Dino over 4 years
    For option 2 you could also list all event types and put break only after the last one.
  • Andrew
    Andrew over 4 years
    @Dino that's certainly possible, and my original statement wasn't specific about where the break should live - i.e. is each one followed by a break, or only the last one? I would, however, advise following each case with a break, as compilers tend to warn about fall-through in switch statements, and if one of them was ever edited to do something meaningful, the fall-through logic may accidentally not be updated, which could cause problems. A few more characters typed, to save ourselves from pain later!
  • ywu
    ywu about 3 years
    It seems to me with '-DSHOW_WARNINGS=1', GNU make version 3.81 doesn't suppress warnings with Option 1. These lines are still seen. 11: warning: enumeration value 'ABC' not explicitly handled in switch [-Wswitch-enum]. 11: note: add missing switch cases