c++ 枚举的底层类型是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1122096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:54:03  来源:igfitidea点击:

What is the underlying type of a c++ enum?

c++enumssizetypeshex

提问by CodeFusionMobile

This may have been answered elsewhere but I could not find a suitable response.

这可能在其他地方得到了回答,但我找不到合适的回答。

I have this code:

我有这个代码:

enum enumWizardPage
{
    WP_NONE = 0x00,  
    WP_CMDID = 0x01,    
    WP_LEAGUES = 0x02,  
    WP_TEAMS = 0x04,    
    WP_COMP = 0x08, 
    WP_DIVISIONS = 0x10,
    WP_FORMULAS = 0x20, 
    WP_FINISHED = 0x40, 
};

Which is legacy and I have to modify it by adding a few new values. The issue is each value must be a unique bit so they may be OR combined to a bitmap.

这是遗留问题,我必须通过添加一些新值来修改它。问题是每个值都必须是唯一的位,因此它们可以或组合成位图。

The values are set using the #x## hex format, but I'm wondering if this is the max it can store? What will be the effect, if any, if I change my code to

这些值是使用 #x## 十六进制格式设置的,但我想知道这是否是它可以存储的最大值?如果我将代码更改为有什么影响,如果有的话

enum enumWizardPage
{
    WP_NONE = 0x0000,  
    WP_CMDID = 0x0001,  
    WP_LEAGUES = 0x0002,    
    WP_TEAMS = 0x0004,  
    WP_COMP = 0x0008,   
    WP_DIVISIONS = 0x0010,
    WP_FORMULAS = 0x0020,   
    WP_FINISHED = 0x0040,   
};

回答by greyfade

The type of a C++ enum is the enum itself. Its range is rather arbitrary, but in practical terms, its underlying type is an int.

C++ 枚举的类型是枚举本身。它的范围相当随意,但实际上,它的基础类型是int.

It is implicitly cast to intwherever it's used, though.

但是,它被隐式转换为int使用它的任何地方。

C++11 changes

C++11 变化

This has changed since C++11, which introduced typed enums. An untyped enumnow is defined as being at least the width of int(and wider if larger values are needed). However, given a typed enumdefined as follows:

自 C++11 引入类型化枚举以来,这种情况发生了变化。无类型的enumnow 被定义为至少是 的宽度int(如果需要更大的值,则更宽)。但是,给定一个 typedenum定义如下:

enum name : type {};

An enumeration of type namehas an underlying type of type. For example, enum : chardefines an enumthe same width as charinstead of int.

类型枚举name的基础类型为type。例如,enum : char定义enumchar而不是相同的宽度int

Further, an enumcan be explicitly scoped as follows:

此外, anenum可以明确地限定如下:

enum class name : type {
    value = 0,
    // ...
};

(Where nameis required, but typeis optional.) An enumdeclared this way will no longer implicitly cast to its underlying type (requiring a static_cast<>) and values must be referenced with a fully-qualified name. In this example, to assign valueto a enumvariable, you must refer to it as name::value.

(哪里name是必需的,但是type可选的。)以enum这种方式声明的An将不再隐式转换为其基础类型(需要 a static_cast<>),并且必须使用完全限定名称引用值。在此示例中,要分配valueenum变量,您必须将其引用为name::value

回答by Kirill V. Lyadvinsky

From N4659 C++ 7.2/5:

N4659 C++ 7.2/5 开始

For an enumeration whose underlying type is not fixed, the underlying type is an integral type that can represent all the enumerator values defined in the enumeration. If no integral type can represent all the enumerator values, the enumeration is ill-formed. It is implementation-defined which integral type is used as the underlying type except that the underlying type shall not be larger than intunless the value of an enumerator cannot fit in an intor unsigned int. If the enumerator-listis empty, the underlying type is as if the enumeration had a single enumerator with value 0.

对于底层类型不固定的枚举,底层类型是一个整型,可以表示枚举中定义的所有枚举器值。如果没有整数类型可以表示所有枚举器值,则枚举格式错误。使用哪种整数类型作为底层类型是实现定义的,除非底层类型不应大于,int除非枚举数的值不能放入intor 中unsigned int。如果enumerator-list为空,则底层类型就好像枚举有一个值为 0 的枚举器。

回答by cube

IIRC its represented as int in memory. But gcc has switch -fshort-enumto make it a shortest integer type that fits all the values, if you need to save space. Other compilers will have something similar.

IIRC 其在内存中表示为 int。但是-fshort-enum如果需要节省空间,gcc 可以切换到使其成为适合所有值的最短整数类型。其他编译器也会有类似的东西。