在 C++ 中将 int 转换为 enum 的通用方法
在C++
中是否有一种通用的方法将int
转换为enum
?
如果int
落在enum
的范围内,它应该返回一个enum
值,否则抛出异常
。有没有办法通用地写它?应该支持多个枚举类型
。
背景:我有一个外部枚举类型,并且无法控制源代码。我想将此值存储在数据库中并检索它。
Is there a generic way to cast int
to enum
in C++
?
If int
falls in range of an enum
it should return an enum
value, otherwise throw an exception
. Is there a way to write it generically? More than one enum type
should be supported.
Background: I have an external enum type and no control over the source code. I'd like to store this value in a database and retrieve it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
显而易见的事情是注释您的枚举:
您需要使数组与
e
保持最新,如果您不是e
的作者,这会很麻烦。正如 Sjoerd 所说,它可能可以通过任何像样的构建系统实现自动化。无论如何,你的得分是 7.2/6:
因此,如果您不是
e
的作者,您可能无法保证e
的有效值实际出现在其定义中。The obvious thing is to annotate your enum:
You need the array to be kept up to date with
e
, which is a nuisance if you're not the author ofe
. As Sjoerd says, it can probably be automated with any decent build system.In any case, you're up against 7.2/6:
So if you aren't the author of
e
, you may or may not have a guarantee that valid values ofe
actually appear in its definition.丑陋的。
现在来说说真正的问题。为什么需要这个?代码很丑陋,不容易编写(*?)并且不容易维护,并且不容易合并到您的代码中。它告诉你它是错误的代码。为何要与之抗争?
编辑:
或者,考虑到枚举是 C++ 中的整数类型:
但这比上面更难看,更容易出错,并且不会按照您的意愿抛出。
Ugly.
Now for the real question. Why do you need this? The code is ugly, not easy to write (*?) and not easy to maintain, and not easy to incorporate in to your code. The code it telling you that it's wrong. Why fight it?
EDIT:
Alternatively, given that enums are integral types in C++:
but this is even uglier that above, much more prone to errors, and it won't throw as you desire.
如果正如您所描述的,这些值位于数据库中,为什么不编写一个代码生成器来读取此表并使用枚举和 to_enum(int) 函数创建 .h 和 .cpp 文件?
优点:
to_string(my_enum)
函数。If, as you describe, the values are in a database, why not write a code generator that reads this table and creates a .h and .cpp file with both the enum and a
to_enum(int)
function?Advantages:
to_string(my_enum)
function.不,C++ 中没有自省,也没有任何内置的“域检查”设施。
No- there's no introspection in C++, nor is there any built in "domain check" facility.
您对此有何看法?
然后,您可以使用我在此处发布的代码来切换值。
What do you think about this one?
You could then use code I posted here to switch on values.
您不应该希望存在您所描述的内容,我担心您的代码设计存在问题。
另外,您假设枚举在一个范围内,但情况并非总是如此:
这不在一个范围内:即使可能,您是否应该检查从 0 到 2^n 的每个整数以查看它们是否与某些匹配枚举的值?
You should not want something like what you describe to exist, I fear there are problems in your code design.
Also, you assume that enums come in a range, but that's not always the case:
This is not in a range: even if it was possible, are you supposed to check every integer from 0 to 2^n to see if they match some enum's value?
如果您准备将枚举值列出为模板参数,则可以在 C++ 11 中使用可变模板来执行此操作。您可以将此视为一件好事,允许您接受不同上下文中有效枚举值的子集;在解析来自外部源的代码时通常很有用。
也许不像您想要的那么通用,但检查代码本身是通用的,您只需要指定一组值即可。这种方法可以处理间隙、任意值等。
If you are prepared to list your enum values as template parameters you can do this in C++ 11 with varadic templates. You can look at this as a good thing, allowing you to accept subsets of the valid enum values in different contexts; often useful when parsing codes from external sources.
Perhaps not quite as generic as you'd like, but the checking code itself is generalised, you just need to specify the set of values. This approach handles gaps, arbitrary values, etc.
C++0x 替代“丑陋”版本,允许多个枚举。使用初始化列表而不是开关,在我看来更干净一些。不幸的是,这并不能解决对枚举值进行硬编码的需要。
C++0x alternative to the "ugly" version, allows for multiple enums. Uses initializer lists rather than switches, a bit cleaner IMO. Unfortunately, this doesn't work around the need to hard-code the enum values.