匿名枚举类
是否可以编写一个匿名枚举类,然后比较它包含的内容?例如。
enum class { APPLE, BANANA } fruitType;
// ...
if (fruitType == fruitType::APPLE)
// ...
Is is possible to write a anonymous enum class and then comparing what it contains? Eg.
enum class { APPLE, BANANA } fruitType;
// ...
if (fruitType == fruitType::APPLE)
// ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不,
fruitType
是一个变量(尽管名称中包含 Type)。您不能使用变量名来访问有关其类型的信息。enum class
的想法是,这些值在定义之外不可见,除非您使用类型名称作为前缀。如果类型没有名称,这将很困难!No,
fruitType
is a variable (despite Type in the name). You cannot use a variable name to access things about its type.The idea with
enum class
is that the values are not visible outside the definition unless you prefix them with the type name. If the type doesn't have a name, this will be difficult!我想说这没有道理。它打破了枚举类的整体思想,引入枚举类是为了解决传统枚举的一些问题(将枚举数导出到周围范围、隐式转换为 int、无法指定底层类型)。如果声明匿名枚举类,则无法访问其枚举器,因为它们无法被限定。所以这没有任何意义。
I'd say this doesn't make sense. It breaks the whole idea of the enum class, which was introduced to solve some of the problems of the traditional enums (export of enumerators to the surrounding scope, implicit conversion to int, impossibility to specify the underlying type). If you declare an anonymous enum class, its enumerators are not accessible, since they cannot be qualified. So it doesn't make any sense.
你不能这样做:
但你可以这样做:
...这可能同样简单。
请注意,您可能想要初始化它:
You can't do this:
But you can do this:
...which is probably just as easy.
Note, that you probably want to initialize it:
你不能。
但我同意,如果能够做到这一点那就太好了。有时,枚举已经是充分嵌套且有限范围的一部分,进一步的限定纯粹是矫枉过正,但您仍然希望禁止转换为 int。因此,您必须做出权衡:选择两个功能或都不选择。
You can't.
But I agree that it would be nice to be able to do this. Sometimes the enum is already part of a sufficiently nested and limited scope and further qualification would be pure overkill, yet you would still like to prohibit conversion-to-int. So you have to make a trade-off: choose both features or neither.
不是这样的。
fruitType::APPLE
不正确,因为fruitType
不是命名空间的名称。但是,如果您只是编写fruitType == APPLE
(不带前缀APPLE
),那就没问题了。Not this way.
fruitType::APPLE
is incorrect, asfruitType
is not the name of a namespace. However, if you simply writefruitType == APPLE
(without prefixingAPPLE
), then it's fine.是的,这是可能。但如果它是匿名的,那么它就无法在其范围之外访问。
编辑:问题的第二部分是一个错误。您不必提及
fruitType::
。然而,即使将其删除后;编译失败。这描述了这一点,它不可用。所以我不确定你的第二个问题。我很久以前就问过类似的问题 。
Yes this is possible. But if it's anonymous then, it won't be accessible outside its whatever scope.
Edit: Your second part of the question is an error. You don't have to mention
fruitType::
. However even after removing it; compilation fails. Which depicts that, it's not usable.So I am not sure about your second question. I had asked a similar question long back.
您实际上可以使用 decltype(fruitType)::BANANA 来代替 FruitType::BANANA,它可以实现从变量访问枚举类类型的技巧。因此,理论上,您可以使用:
但是,C++ 标准明确禁止匿名枚举类,而支持匿名枚举。因此,第一行会给您一个错误,例如“不允许未命名的作用域枚举”。如果您在
enum class
后面插入任何虚拟名称,即使该虚拟名称不再使用,它也会起作用。许多人不鼓励使用
decltype(var)
。但请注意,C++14 向 lambda 引入了auto
参数,而 C++20 也在普通函数中引入了auto
参数。对于这两种情况,decltype() 是访问用于给定参数的编译时类型的唯一方法,因此 decltype() 变得越来越重要最近在普通代码中很常见。它绝对不再局限于复杂的模板类定义和 SFINAE 技巧。Instead of
fruitType::BANANA
, you can actually usedecltype(fruitType)::BANANA
, which will do the trick of making the enum class type accessible from its variable. So, in theory, you could use:However, anonymous enum classes are explicitly forbidden by the C++ standards, while anonymous enums are supported. So, the first line will give you an error like "unnamed scoped enum is not allowed". If you insert any dummy name behind
enum class
, it will work, though, even, if that dummy name is never used again.Many people discourage the use of
decltype(var)
. Note, though, that C++14 introducedauto
parameters to lambdas and C++20 introducedauto
parameters also in normal functions. For both of these,decltype()
is the only way to get access to the compile time type, that was used for a given parameter, sodecltype()
is becoming more common in normal code, lately. It is definitely no longer restricted to complex template class definitions and SFINAE tricks.