How to check if an enum variable is valid?

前端 未结 7 1022
执笔经年
执笔经年 2021-01-11 11:07

I have an enum:

enum myenum{
  typeA,
  typeB,
  typeC
} myenum_t;

Then, a functions is to be called with an enum parameter:



        
7条回答
  •  悲哀的现实
    2021-01-11 11:57

    Unfortunately there isn't a simple way to do it language level (at least with C), you just have to make sure you are using only variables defined via enum.

    Although you could enable one of the following compiler warnings together with -Werror:

    • -Wswitch
    • -Wswitch-default
    • -Wswitch-enum

    This makes build fail if one of the enums is missed inside switch.

提交回复
热议问题