title: How to easily map c++ enums to strings
body: <p>I have a bunch of enum types in some library header files that I'm using, and I want to have a way of converting enum values to user strings - and vice-versa. </p>
  
  <p>RTTI won't do it for me, because the 'user strings' need to be a bit more readable than the enumerations.</p>
  
  <p>A brute force solution would be a bunch of functions like this, but I feel that's a bit too C-like.</p>
  
  <pre><code>enum MyEnum {VAL1, VAL2,VAL3};
  
  String getStringFromEnum(MyEnum e)
  {
    switch e
    {
    case VAL1: return "Value 1";
    case VAL2: return "Value 2";
    case VAL1: return "Value 3";
    default: throw Exception("Bad MyEnum");
    }
  }
  </code></pre>
  
  <p>I have a gut feeling that there's an elegant solution using templates, but I can't quite get my head round it yet.</p>
  
  <p><strong>UPDATE:</strong> Thanks for suggestions - I should have made clear that the enums are defined in a third-party library header, so I don't want to have to change the definition of them.</p>
  
  <p>My gut feeling now is to avoid templates and do something like this:</p>
  
  <pre><code>char * MyGetValue(int v, char *tmp); // implementation is trivial
  
  #define ENUM_MAP(type, strings) char * getStringValue(const type &amp;T) \
   { \
   return MyGetValue((int)T, strings); \
   }
  
  ; enum eee {AA,BB,CC}; - exists in library header file 
  ; enum fff {DD,GG,HH}; 
  
  ENUM_MAP(eee,"AA|BB|CC")
  ENUM_MAP(fff,"DD|GG|HH")
  
  // To use...
  
      eee e;
      fff f;
      std::cout&lt;&lt; getStringValue(e);
      std::cout&lt;&lt; getStringValue(f);
  </code></pre>