这 C++ 吗?转换为枚举有问题吗?
给出以下代码:
enum Options
{
Surf = 0x01,
Out = 0x02
};
Options all = (Options) ( Surf | Out);
- 这个转换有问题吗?
- 如果这个选角有意义,那为什么呢?
根据我的理解,Options只定义了两个变量。值 0x03 有什么意义?
Given the following code:
enum Options
{
Surf = 0x01,
Out = 0x02
};
Options all = (Options) ( Surf | Out);
- Does this casting have problems?
- If this casting make sense, then why?
Based on my understanding, Options only defines two variables. How does the value 0x03 makes sense?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不。
枚举类型
Options
有两个命名枚举器,但它表示的值范围足够大,可以用作包含每个枚举器的位域。简而言之:是的,对这样的位字段使用枚举是有效且定义良好的。
根据此答案的评论中的要求,可以在 C++ 标准 (C++03 7.2/6) 中找到允许这样做的正式语言:
关于这是否是好的风格存在一些争论。当然,可以说,通常假设枚举对象只能存储其枚举器之一,因此这样的代码可能会令人困惑且容易出错。
另一方面,我认为当枚举用作位域时,这通常是非常明显的。通常,此类枚举以
Options
或Flags
后缀或类似名称命名。同样,如果每个枚举器都有一个明显是单个设置位的设置值,则该枚举可能旨在用作位字段。No.
The enumeration type
Options
has two named enumerators, but the range of values that it represents is large enough that it can be used as a bitfield containing each of the enumerators.In short: yes, it is valid and well-defined to use an enumeration for a bitfield like this.
As requested in the comments to this answer, the formal language permitting this can be found in the C++ Standard (C++03 7.2/6):
There is some debate as to whether or not this is good style. Certainly it can be argued that it is often assumed that an enumeration object can only store one of its enumerators, so code like this could be confusing and error-prone.
On the other hand, I would argue that it is usually quite obvious when an enumeration is intended for use as a bitfield. Usually such an enumeration is named with an
Options
orFlags
suffix or something similar. Likewise, if each of the enumerators has a set value that is clearly a single set bit, that enumeration is probably intended for use as a bitfield.Q1>是的。当您似乎需要位掩码时,您正在使用枚举。
Q2>你是对的,这没有意义。使用枚举意味着变量 all 将等于选项中的一项,而不是多项的或组合。
Q1> Yes. You're using an enum when you seemingly want a bitmask.
Q2> You are correct, it doesn't make sense. Using an enum implies that the variable all would be equal to one of the items in Options, not an or'd combination of multiple items..
除了一些语法问题之外,您的代码确实可以工作。
但有点俗气。枚举是整数,但从风格上来说,您应该坚持使用枚举中实际存在的值。
也许对按位集合使用不同的 int。 &我想,分配 2 的幂时要小心。如果你真的想要一个按位集合。
Apart from some syntax troubles, your code does work.
But it's kinda tacky. Enums are ints, but stylewise, you should stick to values that are actually in the enumeration.
Maybe use a different int for the bitwise-collection. & be careful to assign powers of 2, I guess. If you really want a bitwise set.
枚举只是 int 值的占位符,因此当您按位或它们时,您会得到:二进制(4 位表示形式,因此对我来说更容易):
0001| 0010 = 0011 或 3。枚举可以是任何值,甚至是完全无效的值。
enums are just placeholders for int values so when you bitwise or them you get this : in binary (4 bit representation so its easier for me):
0001 | 0010 = 0011 or 3. Enums can be any value even completely invalid ones.
您可能可以将任意整数值分配给枚举类型变量,但这种方式很糟糕。与往常一样,代码的可读性首先,然后是编译器。期望枚举变量仅保存有效的枚举常量。
我会将最后一行更改为:
You might get away with assigning an arbitrary integer value to an
enum
type variable, but it's bad style. As always, code for readability first, and the compiler second. The expectation is that an enum variable will only hold valid enumeration constants.I would change the last line to: