有没有C++库来创建强大的枚举?
理想情况下,我希望以下示例能够工作,但我想其中一些示例无法在 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:
Ideally I would like a following examples to work, but I guess some of it is not implementable 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'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 ();
}
I use them a lot and currently I I write each one from the scratch.
I am aware that a complete generic solution is a dream, so I welcome any partial solutions.
Maybe somebody created a library or a code generator?
Update 1:
This and this questions are related. I'm investigating which issues can be solved with them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是我的结论:
至少它有一些你想要的行为。这是否缺少您需要的东西?
它使用 g++ 4.3.3 编译,并且似乎工作正常。
我做了命名空间的事情,将枚举放在不同的范围内。 (这样红色就不会被占用等)也许你可以将它剖析成你可以使用的东西? :)
如果你想要 Color::Color 在该命名空间之外,你可以这样做:
但不幸的是,名称 Color 被命名空间占用了。
This is what I figured out:
Atleast it has some of the behaviour you wanted. Does this lack something that you need?
It compiles with g++ 4.3.3, and seems to work ok.
I did the namespace thing to put the enums under a different scope. (so that Red's not taken etc.) Maybe you can dissect it into something you could use? :)
If you want Color::Color outside that namespace, you could do:
But the name Color is unfortunately occupied by the namespace.
我也讨厌 C++ 中枚举的实际实现。
我最终推出了自己的模板来尝试自动化,但这并不完全令人满意目前(特别是因为它需要每个枚举的模板专门化,因此不能用于嵌套在类/结构中的枚举:/)
无论如何,我使用的想法是:
目前我使用了“真实”枚举,但从你所说的来看,我可能会想到拥有静态实例......尽管它给枚举编写者带来了另一个负担......
I also loathe the actual implementation of enums in C++.
I ended up rolling my own template to try and automate, but it's not completely satisfying at the moment (especially because it requires template specialization for each enum, and so cannot be used for enums nested in a class / struct :/)
Anyway, the idea I used was:
For the moment I used the 'true' enum, but from what you said I might think of having static instances... though it puts yet another burden on the enum writer...