Using default in a switch statement when switching over an enum

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

    I am not a C++ guy. But, in C#, I whould have written it like

    enum Enum
    {
        Enum_One,
        Enum_Two
    };
    
    
    Special make_special( Enum e )
    {
    
        if(Enums.IsDefined(typeof(Enum),(int)e))
        {
           switch( e )
           {
              case Enum_One:
                  return Special( /*stuff one*/ );
              case Enum_Two:
                  return Special( /*stuff two*/ );
           }
        }
        // your own exception can come here.
        throw new ArgumentOutOfRangeException("Emum Value out of range");
    
    }
    

提交回复
热议问题