Easy way to use variables of enum types as string in C?

前端 未结 19 2039
太阳男子
太阳男子 2020-11-22 08:47

Here\'s what I am trying to do:

typedef enum { ONE, TWO, THREE } Numbers;

I am trying to write a function that would do a switch case sim

相关标签:
19条回答
  • 2020-11-22 09:06

    If you are using gcc, it's possible to use:

    const char * enum_to_string_map[]={ [enum1]='string1', [enum2]='string2'};
    

    Then just call for instance

    enum_to_string_map[enum1]
    
    0 讨论(0)
  • 2020-11-22 09:09

    KISS. You will be doing all sorts of other switch/case things with your enums so why should printing be different? Forgetting a case in your print routine isn't a huge deal when you consider there are about 100 other places you can forget a case. Just compile -Wall, which will warn of non-exhaustive case matches. Don't use "default" because that will make the switch exhaustive and you wont get warnings. Instead, let the switch exit and deal with the default case like so...

    const char *myenum_str(myenum e)
    {
        switch(e) {
        case ONE: return "one";
        case TWO: return "two";
        }
        return "invalid";
    }
    
    0 讨论(0)
  • 2020-11-22 09:09

    Because I prefer not to use macros for all the usual reasons, I used a more limited macro solution that has the advantage of keeping the enum declaration macro free. Disadvantages include having to copy paste the macro defintion for each enum, and having to explicitly add a macro invocation when adding values to the enum.

    std::ostream& operator<<(std::ostream& os, provenance_wrapper::CaptureState cs)
    {
    #define HANDLE(x) case x: os << #x; break;
        switch (cs) {
        HANDLE(CaptureState::UNUSED)
        HANDLE(CaptureState::ACTIVE)
        HANDLE(CaptureState::CLOSED)
        }
        return os;
    #undef HANDLE
    }
    
    0 讨论(0)
  • 2020-11-22 09:11

    If the enum index is 0-based, you can put the names in an array of char*, and index them with the enum value.

    0 讨论(0)
  • 2020-11-22 09:14

    Here is a solution using macros with the following features:

    1. only write each value of the enum once, so there are no double lists to maintain

    2. don't keep the enum values in a separate file that is later #included, so I can write it wherever I want

    3. don't replace the enum itself, I still want to have the enum type defined, but in addition to it I want to be able to map every enum name to the corresponding string (to not affect legacy code)

    4. the searching should be fast, so preferably no switch-case, for those huge enums

    https://stackoverflow.com/a/20134475/1812866

    0 讨论(0)
  • 2020-11-22 09:15

    By merging some of the techniques over here I came up with the simplest form:

    #define MACROSTR(k) #k
    
    #define X_NUMBERS \
           X(kZero  ) \
           X(kOne   ) \
           X(kTwo   ) \
           X(kThree ) \
           X(kFour  ) \
           X(kMax   )
    
    enum {
    #define X(Enum)       Enum,
        X_NUMBERS
    #undef X
    } kConst;
    
    static char *kConstStr[] = {
    #define X(String) MACROSTR(String),
        X_NUMBERS
    #undef X
    };
    
    int main(void)
    {
        int k;
        printf("Hello World!\n\n");
    
        for (k = 0; k < kMax; k++)
        {
            printf("%s\n", kConstStr[k]);
        }
    
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题