Using default in a switch statement when switching over an enum

后端 未结 16 1267
一个人的身影
一个人的身影 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:55

    Because it may help to know what the unexpected value of the enum was, write your own BADENUM(Enum) macro, along the lines of:

    #define _STRIZE(x) _VAL(x)
    #define _VAL(x) #x
    extern void  __attribute__ ((noreturn)) AssertFail(const char *Message);
    #define ASSERT(Test) ((Test) ? (void)0 : AssertFail(__FILE__ ":" _STRIZE(__LINE__) " " #Test))
    
    extern void  __attribute__ ((noreturn)) BadEnum(const char *Message, const long unsigned Enum);
    #define BADENUM(Enum)  BadEnum(__FILE__ ":" _STRIZE(__LINE__), (u32)Enum))
    

提交回复
热议问题