理想情况下,我希望以下示例能够正常工作,但我想其中一些示例无法在 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's 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 ();
}
我经常使用它们,目前我从头开始编写每一个。我知道一个完整的通用解决方案是一个梦想,所以我欢迎任何部分解决方案。也许有人创建了一个库或代码生成器?
更新1: