Debugging MSVC interception

Is there a quick way to output the names of enumerated values? I suppose you know what I mean and in general it is not possible, since of course all this data becomes irrelevant during the compilation process, but I am using MSVC in debug mode, so is this possible?

+2


a source to share


4 answers


Metamacros is causing all sorts of chaos on Intellisense and the like, but they can make it easier ...

#define MY_ENUMS(e_) \
   e_(Enum_A), \
   e_(Enum_B), \
   e_(Enum_C), \

#define ENUM_EXPANDER(e_)  e
enum MyEnums
{
   MY_ENUMS(ENUM_EXPANDER)
   CountOfMyEnums
};

#define STRING_EXPANDER(e_)  #e_
const char* g_myEnumStrings[] =
{
   MY_ENUMS(STRING_EXPANDER)
};

      

Maybe even



#define CASE_EXPANDER(e_)  case e_: return #e_;
const char* GetEnumName(MyEnums e)
{
   switch (e)
   {
      MY_ENUMS(CASE_EXPANDER)

   default:
      return "Invalid enum value";
   }
}

      

Various "expander macros" can be used to populate maps or other data structures of your choice. I used this horror to parse enums from config files (so the person who created the config file could use the enumeration, not the index).

+1


a source


I just put the enumeration names in the lookup table (or you could use map<>

) with the enum value as the key and have a function doing the lookup.

It's low tech, but usually not too much of a pain.



In some projects I will have a weird header / macro layout that could build an enum definition using one declaration-like element for each enum name. My opinion on how this technique works unfolds between "comfy" or "shreds".

+1


a source


This is a common problem in C ++ and is solved with the "type enumeration pattern". This is usually done using some crazy precompiler definitions or code generators. A quick search on "Typafe enum pattern C ++" can give you these ways. Personally, I have my own code generator for C ++ enums that runs as a custom MSVC build step for h files with enums.

+1


a source


Unfortunately not. All enumeration names are lost by the compiler. The PDB file has them, so the debugger can handle it, but otherwise the only way to do it is to write a function that executes the switch and returns a string.

0


a source







All Articles