枚举的基础 int 是否有保证宽度?

发布于 2024-12-07 15:33:07 字数 836 浏览 1 评论 0原文

在 N2347 的示例中,新强类型枚举 (C++11) 与当前 (C++03) 相比的一个优势是通过一个“旧”枚举的示例来解释的:

enum Version { Ver1 = 1, Ver2 = 2 };
struct Packet {
    Version ver;
    // ... more data ...
    // bad, size can vary by implementation
   Version getVersion() const { return ver; }
};

我想知道:这里的“变化”意味着编译器甚至可以选择一种8位表示形式,即枚举元素仅占用一个单词?或者是否有最小长度保证并且选择只能在intlong之间?

换句话说,如果我有一个像这样的 Version 枚举:

enum VersionXyz { Ver1 = 100, Ver2 = 200 };
struct Packet {
    VersionXyz ver;
    // ... more data ...
};

这是否可能只需要 Packet 中的 8bit 。软件更新后:

enum VersionXyz { Ver1 = 100, Ver2 = 200, Ver3=300 };

现在 Packet 不再具有相同的大小,因为枚举已增长?

In the example in N2347 one advantage if the new stongly typed enums (C++11) compared the current (C++03) is explained by an example with an "old" enum:

enum Version { Ver1 = 1, Ver2 = 2 };
struct Packet {
    Version ver;
    // ... more data ...
    // bad, size can vary by implementation
   Version getVersion() const { return ver; }
};

I wonder: means "varying" here that the compiler can even choose a 8bit representation, i.e. that the enum-elements occupy only one word? Or is there a minimum length guaranteed and the choice may only be between int and long?

In other words, if I have a Version enum like this:

enum VersionXyz { Ver1 = 100, Ver2 = 200 };
struct Packet {
    VersionXyz ver;
    // ... more data ...
};

Is it possible that this takes only 8bit in Packet. And after a software update to:

enum VersionXyz { Ver1 = 100, Ver2 = 200, Ver3=300 };

now Packet has not the same size anymore because the enum has grown?

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

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

发布评论

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

评论(2

淡墨 2024-12-14 15:33:07

C++03 标准的相关段落是 §7.2.5:

枚举的基础类型是可以表示所有枚举器值的整型
枚举中定义。 使用哪种整型作为基础类型是由实现定义的
对于枚举
,但基础类型不得大于 int,除非枚举数的值不能放入 int 或 unsigned int。如果枚举器列表为空,则基础类型为
就好像枚举有一个值为 0 的枚举数。应用于枚举类型、枚举类型的对象或枚举数的 sizeof() 值是应用于
底层类型。

所以它是实现定义的。唯一可以保证的是,如果您定义一个所有值都适合 int 的枚举,则枚举类型的大小不会大于 int。 (但是由于 int 的大小也是实现定义的,所以这并没有给你太多信息。)

The relevant paragraph of the C++03 standard is §7.2.5:

The underlying type of an enumeration is an integral type that can represent all the enumerator values
defined in the enumeration. It is implementation-defined which integral type is used as the underlying type
for an enumeration
except that the underlying type shall not be larger than int unless the value of an enumerator cannot fit in an int or unsigned int. If the enumerator-list is empty, the underlying type is
as if the enumeration had a single enumerator with value 0. The value of sizeof() applied to an enumeration type, an object of enumeration type, or an enumerator, is the value of sizeof() applied to the
underlying type.

So it's implementation defined. The only thing you are guaranteed is that if you define an enum where all values fit into an int, then the enumeration type's size won't be greater than int. (But since int's size is also implementation-defined, that doesn't give you much.)

清醇 2024-12-14 15:33:07

如果您知道您的枚举列表只会达到一定的大小,那么在使用时可能值得对其进行类型转换,并且还可以断言是否超过了该大小,至少这就是我在发送数据包时使用枚举的方式

If you know that your enum list is only ever going to be a certain size it might be worth type casting it when used, and also have an assert for if that size is ever exceeded, at least thats how I use enums when sending packets

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