0

I have a tool that autogenerates a lot of C++ code. One thing it generates is a lot of enums. For example:

   typedef enum InsId
    {
        kInsH  = 0,      
        kInsM1  = 1,      
        kInsM2  = 2,      
        kNumIns  = 3     
    } InsId;

I have some code that has to convert strings to the aforementioned enums: "kInsH" -> InsId::kInsH. A map can be used for that (as described in many answers):

const std::unordered_map<InsId, std::string> id_map_{{InsId::kInsH, "kInsH"},
                                                     {InsId::kInsM1, "kInsM1"},
                                                     {InsId::kInsM2, "kInsM2"}};

The question is: Is there a way to generate those strings at the compile time from the enum field names?

Thank you.

ilya1725
  • 4,496
  • 7
  • 43
  • 68
  • Have you seen https://stackoverflow.com/questions/28828957/enum-to-string-in-modern-c11-c14-c17-and-future-c20? – cigien Aug 17 '20 at 22:29
  • Magic header file. But I'm not a fan. (Because I hate the preprocessor.) – Eljay Aug 17 '20 at 22:30

0 Answers0