Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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,    }; 
like image 583
CodeFusionMobile Avatar asked Jul 13 '09 21:07

CodeFusionMobile


People also ask

What is underlying type of enum?

Each enum type has a corresponding integral type called the underlying type of the enum type. This underlying type shall be able to represent all the enumerator values defined in the enumeration. If the enum_base is present, it explicitly declares the underlying type.

What is underlying type in C?

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.

What is enum data type in C?

Enumeration or Enum in C is a special kind of data type defined by the user. It consists of constant integrals or integers that are given names by a user. The use of enum in C to name the integer values makes the entire program easy to learn, understand, and maintain by the same or even different programmer.

What is default underlying data type of an enum?

The default value of an enumeration type E is the value produced by expression (E)0 , even if zero doesn't have the corresponding enum member.


2 Answers

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.

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

C++11 changes

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

enum name : type {}; 

An enumeration of type name has an underlying type of type. For example, enum : char defines an enum the same width as char instead of int.

Further, an enum can be explicitly scoped as follows:

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

(Where name is required, but type is optional.) An enum declared 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 value to a enum variable, you must refer to it as name::value.

like image 154
greyfade Avatar answered Oct 11 '22 10:10

greyfade


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

like image 41
Kirill V. Lyadvinsky Avatar answered Oct 11 '22 09:10

Kirill V. Lyadvinsky