这 C++ 吗?转换为枚举有问题吗?

发布于 2024-10-12 06:17:34 字数 241 浏览 1 评论 0原文

给出以下代码:

enum Options
{
    Surf     = 0x01,
    Out      = 0x02
};

Options all = (Options) ( Surf | Out);
  1. 这个转换有问题吗?
  2. 如果这个选角有意义,那为什么呢?

根据我的理解,Options只定义了两个变量。值 0x03 有什么意义?

Given the following code:

enum Options
{
    Surf     = 0x01,
    Out      = 0x02
};

Options all = (Options) ( Surf | Out);
  1. Does this casting have problems?
  2. If this casting make sense, then why?

Based on my understanding, Options only defines two variables. How does the value 0x03 makes sense?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

余罪 2024-10-19 06:17:34

这个选角有问题吗?

不。

如果这个选角有意义,那为什么呢?据我了解,Options只定义了两个变量,0x03这个值有什么意义?

枚举类型 Options 有两个命名枚举器,但它表示的值范围足够大,可以用作包含每个枚举器的位域。

简而言之:是的,对这样的位字段使用枚举是有效且定义良好的。

根据此答案的评论中的要求,可以在 C++ 标准 (C++03 7.2/6) 中找到允许这样做的正式语言:

对于一个枚举,其中 emin 是最小的枚举数,emax 是最大的,值枚举的值是 bminbmax 范围内的基础类型的值,其中 bminbmax 分别是可以存储 < 的最小位域的最小值和最大值em>eminemax

可以定义一个具有未由任何枚举器定义的值的枚举。

关于这是否是好的风格存在一些争论。当然,可以说,通常假设枚举对象只能存储其枚举器之一,因此这样的代码可能会令人困惑且容易出错。

另一方面,我认为当枚举用作位域时,这通常是非常明显的。通常,此类枚举以 OptionsFlags 后缀或类似名称命名。同样,如果每个枚举器都有一个明显是单个设置位的设置值,则该枚举可能旨在用作位字段。

Does this casting have problems?

No.

If this casting make sense, then why? Based on my understanding, Options only defines two variables, how the value 0x03 makes sense?

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):

For an enumeration where emin is the smallest enumerator and emax is the largest, the values of the enumeration are the values of the underlying type in the range bmin to bmax, where bmin and bmax are, respectively, the smallest and largest values of the smallest bit-field that can store emin and emax.

It is possible to define an enumeration that has values not defined by any of its enumerators.

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 or Flags 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.

許願樹丅啲祈禱 2024-10-19 06:17:34

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..

初相遇 2024-10-19 06:17:34

除了一些语法问题之外,您的代码确实可以工作。
但有点俗气。枚举是整数,但从风格上来说,您应该坚持使用枚举中实际存在的值。

也许对按位集合使用不同的 int。 &我想,分配 2 的幂时要小心。如果你真的想要一个按位集合。

typedef enum Options
{
Surf     = 0x01,
Out      = 0x02
} Options;

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.

typedef enum Options
{
Surf     = 0x01,
Out      = 0x02
} Options;
花间憩 2024-10-19 06:17:34

枚举只是 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.

糖粟与秋泊 2024-10-19 06:17:34

您可能可以将任意整数值分配给枚举类型变量,但这种方式很糟糕。与往常一样,代码的可读性首先,然后是编译器。期望枚举变量仅保存有效的枚举常量。

我会将最后一行更改为:

int all = ( Surf | Out); 

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:

int all = ( Surf | Out); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文