#string enum

3 messages · Page 1 of 1 (latest)

stray quiver
#

Hi, I want to have an enum that instead of it's members referring to int etc it refers to a const char*/std::string

Something like this:

enum class string_enum {
    A = "A",
    B = "B"
};

Do I have to use a struct or is there a better option?

marsh gull
#

You can either use a map (unordered, flat, whatever) taking enums values as keys and the corresponding strings as values, or use magic_enum to benefit from reflection at compile time

#

Note that magic_enum has a few restrictions though (you can read them in the README of the library)