c# - 枚举类型宽度?

标签 c# c++ c size enumeration

快速提问。枚举类型有多广泛?它们是表示枚举所需的最小宽度还是都是枚举整数?如果它们是整数,您可以更改枚举的宽度,还是每次出现都必须键入 cast?

最佳答案

(这是针对 C++ 的)

来自标准:

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.

在 C++0x 中,您可以像下面这样定义枚举的基础类型:

enum foo : unsigned int { bar, baz };

另请注意,新的强类型枚举(“枚举类”)具有默认的基础类型 int。

关于c# - 枚举类型宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5545236/

相关文章:

c# - SqlBulkCopy 成功但未插入任何记录

C#,如果 'a letter' 后跟 'a letter',则检查字符串

c# - 替代多个 String.Replaces

c - 对非阻塞recvfrom的单次调用

c# - 排序重定向到另一个页面

C++ 获取 "Segmentation Fault"Linux x64 的文件名和行号

java - 为什么我的 native C++ 代码在 Android 上的运行速度比 Java 慢得多?

c++ - 如何从参数包中定义值类型的元组

c - 将节点插入双链表时出现段错误

c - 好的 C 绘图库?