In Java, enums come with several capabilities missing from C/C++ enums. I would like to be able to define enums with, say, some macro ENUM(Color, Red, Green, Blue)
or ENUM(Color, {Red, Green, Blue} )
or what-not, and have an enum defined with these capabilities implemented somehow.
Specifically, I would like to be able to do the following:
for (Color p : Color::values()) { }
and
Color color(Red);
cout << "My face shone a bright " << color;
and
Color color = valueOf<Color>("Green");
/* ... */
cout << "It ain't easy being " << color;
Notes:
- Bonus points if the solution lets me specify the (integral) value for each enum value (e.g. Red is 2, Green is 5, Blue is 19).
- Bonus points if the solution lets me specify the 'inherited type', a-la
enum Color : unsigned char { Red, Green, Blue};
- Suggestions using Boost are welcome, although I would rather stick to just the standard library.
- I'm not 'married' to macros.
- This question relates to this one, but is not the same. That question is concerned mainly with allowing multiple data fields, while I don't care about that and am satisfied with a proper single-type enum.