Using default in a switch statement when switching over an enum

后端 未结 16 1300
一个人的身影
一个人的身影 2020-12-15 02:57

What is your procedure when switching over an enum where every enumeration is covered by a case? Ideally you\'d like the code to be future proof, how do you do that?

<
16条回答
  •  青春惊慌失措
    2020-12-15 03:52

    I would put an assert.

    Special make_special( Enum e )
    {
        switch( e )
        {
            case Enum_One:
                return Special( /*stuff one*/ );
    
            case Enum_Two:
                return Special( /*stuff two*/ );
    
            default:
                assert(0 && "Unhandled special enum constant!");
        }
    }
    

    Not handling an enumeration value, while the intention is to cover all cases, is an error in the code that needs to be fixed. The error can't be resolved from nor handled "gracefully", and should be fixed right away (so i would not throw). For having the compiler be quiet about "returning no value" warnings, call abort, like so

    #ifndef NDEBUG
    #define unreachable(MSG) \
      (assert(0 && MSG), abort())
    #else
    #define unreachable(MSG) \
      (std::fprintf(stderr, "UNREACHABLE executed at %s:%d\n", \
                    __FILE__, __LINE__), abort())
    #endif 
    
    Special make_special( Enum e )
    {
        switch( e )
        {
            case Enum_One:
                return Special( /*stuff one*/ );
    
            case Enum_Two:
                return Special( /*stuff two*/ );
    
            default:
                unreachable("Unhandled special enum constant!");
        }
    }
    

    No warning by the compiler anymore about a return without value, because it knows abort never returns. We immediately terminate the failing program, which is the only reasonable reaction, in my opinion (there is no sense in trying to continue to run a program that caused undefined behavior).

提交回复
热议问题