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,
};
Best Answer
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 ofint
(and wider if larger values are needed). However, given a typedenum
defined as follows:An enumeration of type
name
has an underlying type oftype
. For example,enum : char
defines anenum
the same width aschar
instead ofint
.Further, an
enum
can be explicitly scoped as follows:(Where
name
is required, buttype
is optional.) Anenum
declared this way will no longer implicitly cast to its underlying type (requiring astatic_cast<>
) and values must be referenced with a fully-qualified name. In this example, to assignvalue
to anenum
variable, you must refer to it asname::value
.