Is there a C ++ library for building strong Enums?

Ideally, I would like the following examples to work, but I think some of them are not implemented in C ++.

{ typedef StrongEnum<Red=0, Green=1, Blue=2> Color; // not a C++ syntax Color c = Color::Red; // static const Color d; //error: default constructor is private Color d = c; Color e = Color::OfInt(5); // ifdef DEBUG - Runtime error: Enum out of range int sum = 0; // I do have these macros, but separate for each enum - FOREACH_COLOR(c) FOREACH_ENUM (Color c) { sum += c.ToInt (); } ArrayMap<Color, string> map; // Internally this is const size array, possible map [Color::Red] = "red"; // because Color have static const Limit = 3 inisde. // Advanced: EnumPair does bitpacking. // currently I implement it manually for every pair of Enum I need. typedef EnumPair <door=Color, window=Color> ColorPair; // I guess I can't get this, can I? ColorPair pair (door = Color::Red, window = Color::Green); // I guess I can't give the labels here or one line above, can I? Color w = pair.window; Color w = pair.window (); } 

I use a lot of them, and currently I write each from scratch. I know that a complete general solution is a dream, so I welcome any partial solutions. Maybe someone created a library or code generator?

Update 1:

This and this are related. I study what problems can be solved with them.

+4
source share
2 answers

Here is what I understood:

 #include <cstdio> #include <string> #include <map> namespace Color { typedef enum { Red = 0, Green = 1, Blue = 2 } Color; Color colors[] = {Red, Green, Blue}; // same order as above, //to preserve index. //int colors_len = sizeof(colors)/sizeof(Color); // (if you want to check for valid values) static inline Color OfInt(int value) { // if(value >= colors_len) do error thing; return colors[value]; } } int main() { Color::Color c = Color::Red; printf("%d,", c); c = Color::OfInt(1); printf("%d,", c); c = Color::Blue; printf("%d\n", c); std::map<Color::Color, std::string> map; map[Color::Red] = "red"; return 0; } 

At least he has some behavioral needs. Are you missing something you need?

It compiles with g ++ 4.3.3 and seems to work fine.

I made a namespace to translate the enumerations to another area. (so that Red is not taken, etc.) Maybe you can open it into something that you could use? :)

If you want Color :: Color outside this namespace, you can do:

 typedef Color::Color ColorEnum; 

But unfortunately, the name Color occupies the namespace.

+2
source

I also hate the actual implementation of enums in C ++.

  • Automatic conversion to integral types
  • No value check: a free range check is performed to see if the value matches the integral type selected for storing it.
  • Serialization: serializing as int hurts> you need to keep the old values, even if you no longer use them, and you should add new values ​​only at the end
  • Unable to iterate, you need to override operators

I finished my own template to try to automate, but it is not completely satisfactory at the moment (especially because it requires specialized specialization for each enumeration and therefore cannot be used for enumerations nested in the / struct: / class)

Anyway, I used this idea:

  • A static map for storing values ​​and their "serialization values" (which in my case is a simple string, because I don't value space so much and prefer readability)
  • A transfer class that simply contains an iterator on the map, an 'end' representing an uninitialized / invalid value.

At the moment I used the "true" enumeration, but from what you said, I could think about the presence of static instances ... although this puts one more burden on the author of the enumeration ...

0
source

All Articles