   enum to string and string to enum
  What's the best way to convert an enum to a string and vice-versa in the current C++ standard? I mean I could do it with two hash tables but was wondering if there are other options.   
  Hash tables. Or switch statement. Or bunch of if/elses.   ;  Yeah, I'm going to use unordered_maps ... currently reading the internets about clever preprocessor tricks to get out of having to manually list the enums twice i.e. I'm procrastinating actually working this morning...   ;  I am not sure if this is a good suggestion, maybe someone can comment on this:but one could use the pre-processor directive :#define toString(EnumName) #EnumNameThat works if you explicitly define the name for the enum.   ;  There's no good way.  But why a hash map?  Seems like overkill unless you need the performance.Stamping them out with the preprocessor has its own annoying issues.  I put my enums in a different namespace and manually type out the strings.  Has its own problems, including the manual typing and possibilities for errors, and requires the values to start at 0 and go up.  However, as I said, its all I need 95% of the time.namespace Color{    enum Enum    {   Red,   Green,   Blue    };    const char* const ToStr[] ={   "Red",   "Green",   "Blue"    };}You then declare variables using Color::Enum, access the values using Color::Red, and get the string using Color::ToStr[Color::Red].   ;  A hash map would actually be slower than gekko's method. The only thing I don't like about gekko's method is the possibility for errors. I generally use a switch statement, which is also quite fast:#define _STRINGIFY_(x) case x: return #xstruct Color{    enum Enum    {   Red,   Green,   Blue    };    static const char* const ToStr(Enum value)    {   switch (value)   {  _STRINGIFY_(Red);  _STRINGIFY_(Green);  _STRINGIFY_(Blue);   }   return "INVALID";    };};#undef _STRINGIFY_;Quote:Original post by gekkoThere's no good way.  But why a hash map?  Seems like overkill unless you need the performance. [...]namespace Color{    enum Enum    {   Red,   Green,   Blue    };    const char* const ToStr[] ={   "Red",   "Green",   "Blue"    };}That only works in the enum->string direction. Also to be safe you'd have to define the Enum like the following in order to use an Enum as an index into ToStr[]:enum Enum {   Red = 0,   Green = 1,   Blue = 2}or at least have to have the initial Red = 0 -- I'm not sure what the standard says -- anyway my code doesn't care about the enums' integral values so I'd rather not define them, and in the string->enum direction you need some kind of map unless you want to do a linear search on the the ToStr[] array.;  Standard says they start at 0. You're no more safe by setting the values explicitly. You're just more likely to typo something.   ;Quote:Original post by TelastynStandard says they start at 0. You're no more safe by setting the values explicitly. You're just more likely to typo something.They start at zero even if I don't specify the intial one? (Because I'm working on a clever preprocessor trick and it will mess it up if I have to specify the integral zero value of the first enum)   ;Quote:Original post by jwezorekQuote:Original post by TelastynStandard says they start at 0. You're no more safe by setting the values explicitly. You're just more likely to typo something.They start at zero even if I don't specify the intial one? Yes.Quote:(Because I'm working on a clever preprocessor trick and it will mess it up if I have to specify the integral zero value of the first enum)Please, don't. 'Clever' is a code smell. The resultant code is inevitably fragile, unmaintainable garbage done to save a little typing. Take the time, do it right. You'll save time and sanity in the long run.