   Stringizing C Enums
  Comments for the article Stringizing C++ Enums
  Just noticed a small typo:The first line of the code listing in the 'How does it actually work?' section istemplate << struct EnumString>Master> :It should actually betemplate <> struct EnumString<Master> :Thanks,Francis Xavier;  Hey, that's really nice work! It had never occurred to me that something like that was possible in C++. Thanks for the article!   ;Quote:Original post by FrancisXavierJust noticed a small typoFixed;  Nice article!I'd actually say that the biggest disadvantage of this implementation is having to maintain two identical (redundant) enumerations -- it's error prone, and a bit of a hassle, especially for large lists.Had you considered using x-macros? They solve the enum redundancy issue, as well as provide constant time enum-to-string lookups (as a bonus). They have their own set of disadvantages, but I personally think the trade-off is worth it here.You could even combine your method with x-macros (in order to define the identifiers only once), and maintain the templated approach with C++ containers if you wanted.Cheers!;  Great work. I've implemented it in my engine, with some changes, and it simplified some sections of my code ALOT!I used it in sections of my code that read script files and translate string tokens to the corresponding enum values. The solution Francis proposed allowed me to avoid a lot of if-else statements.Thanks, Francis.   ;Quote:Original post by ThrustGoblinNice article!I'd actually say that the biggest disadvantage of this implementation is having to maintain two identical (redundant) enumerations -- it's error prone, and a bit of a hassle, especially for large lists.Thanks! You're right, it is somewhat of a hassle; but this is the neatest syntax I could come up with. :(Quote:Original post by ThrustGoblinHad you considered using x-macros? They solve the enum redundancy issueThe article's aim is to also provide support for existing enumerations (like those from commonly used libraries). Besides, IMHO X-Macros syntax is quite confusing to beginners.Quote:Original post by ThrustGoblinas well as provide constant time enum-to-string lookups (as a bonus).I'm probably missing something, but does it work also for enumerators with non-contiguous values?;Quote:Original post by jcabeleiraGreat work. I've implemented it in my engine, with some changes, and it simplified some sections of my code ALOT!I used it in sections of my code that read script files and translate string tokens to the corresponding enum values. The solution Francis proposed allowed me to avoid a lot of if-else statements.Thanks, Francis.Thanks, I'm happy that it was of use to someone other than myself. :)   ;  It's true the X-Macro syntax is a bit confusing at first, which is a negative aspect. But as with actual design patterns, you just need to learn it once. They are very useful and powerful in certain situations, and reduce redundancy which is what makes them worth considering. I've seen a lexical analyzer made using x-macros, for example (wouldn't necessarily suggest that, but it is definitley possible).Using your list of masters as an example, you could have something like this (off top of my head, haven't actually compiled this):enum Master{#define DeclareMaster(name, weight) name,#include "MasterList.h"#undef DeclareMaster};const char * GetMasterName(const Master master){ switch(master) {#define DeclareMaster(name, weight) case name: return #name;#include "MasterList.h"#undef DeclareMaster   default: return "";   }}unsigned int GetMasterWeight(const Master master){ switch(master) {#define DeclareMaster(name, weight) case name: return weight;#include "MasterList.h"#undef DeclareMaster   default: return 0;   }}And the MasterList.h header would simply look like:// DeclareMaster(name, weight)DeclareMaster(Tigress, 5)DeclareMaster(Viper, 3)DeclareMaster(Monkey, 4)DeclareMaster(Mantis, 3)DeclareMaster(Crane, 2)The MasterList.h is meaningless by itself, since it acts like a template. The ID for each master implied (by the order in enum), and the weight/names are constant-time lookups. You could easily add more properties to the masters, as additional parameters to the DeclareMaster() macro... like height, strength etc.Anyway, just a suggestion. Article was still very good :)   ;Quote:Original post by ThrustGoblinIt's true the X-Macro syntax is a bit confusing at first, which is a negative aspect. But as with actual design patterns, you just need to learn it once. They are very useful and powerful in certain situations, and reduce redundancy which is what makes them worth considering. I've seen a lexical analyzer made using x-macros, for example (wouldn't necessarily suggest that, but it is definitley possible).Thanks for the code snippet and info on X-Macros; it looks like a very useful method.Quote:Original post by ThrustGoblinArticle was still very good :)Thank you :)The main idea behind the article was to provide a consistent method for both new as well as existing enumerations. In the latter case, the issue of having to re-declare the enumeration becomes irrelevant. However, if you don't need string support for existing enumerations or don't mind the consistency of usage between the latter and the former, then the method you have described is very effective.[Edited by - FrancisXavier on November 5, 2008 10:32:55 AM]