Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Underlying type of a C++ enum in C++0x

Tags:

c++

enums

c++11

I've been trying to read a bit of the C++ standard to figure out how enum's work. There's actually more there than I originally thought.

For a scoped enumeration, it's clear that the underlying type is int unless otherwise specified with an enum-base clause (it can be any integral type).

enum class color { red, green, blue};  // these are int 

For unscoped enumerations, it seems like the underlying type can be any integral type that will work and that it won't be bigger than an int, unless it needs to be.

enum color { red, green, blue};  // underlying type may vary 

Since the underlying type of unscoped enumarations are not standardized, what's the best way of dealing with serializing instances of one? So far, I've been converting to int when writing then serializing into an int and setting my enum variable in a switch when reading, but it seems a bit clunky. Is there a better way?

enum color { red, green, blue }; color c = red; // to serialize archive << (int)c; // to deserialize int i; archive >> i; switch(i) {   case 0: c = red; break;   case 1: c = green; break;   case 2: c = blue; break; } 
like image 494
criddell Avatar asked May 12 '09 15:05

criddell


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 the default underlying type of each element in an enum?

The default underlying type of the enumeration elements is int. By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1. Enums are enumerated data type in C#.

What is the data type of enum in C++?

In C++ programming, enum or enumeration is a data type consisting of named values like elements, members, etc., that represent integral constants. It provides a way to define and group integral constants. It also makes the code easy to maintain and less complex.


1 Answers

enum class is a C++0x feature, it is not present in C++03.

In standard C++, enumerations are not type-safe. They are effectively integers, even when the enumeration types are distinct. This allows the comparison between two enum values of different enumeration types. The only safety that C++03 provides is that an integer or a value of one enum type does not convert implicitly to another enum type. Additionally, the underlying integral type, the size of the integer, cannot be explicitly specified; it is implementation defined. Lastly, enumeration values are scoped to the enclosing scope. Thus, it is not possible for two separate enumerations to have matching member names. C++0x will allow a special classification of enumeration that has none of these issues. This is expressed using the enum class declaration

Examples (from the wikipedia article):

enum Enum1;                   //Illegal in C++ and C++0x; no size is explicitly specified. enum Enum2 : unsigned int;    //Legal in C++0x. enum class Enum3;             //Legal in C++0x, because enum class declarations have a default type of "int". enum class Enum4: unsigned int; //Legal C++0x. enum Enum2 : unsigned short;  //Illegal in C++0x, because Enum2 was previously declared with a different type. 

As for the serialization part (which I think was not part of the original question), I prefer to create a helper class that translates enum items into their string equivalent (and back), as the names are usually more stable than the integer values they represent, as enum items can be (and sometimes are) reordered without changing the code behavior.

like image 118
lothar Avatar answered Sep 22 '22 09:09

lothar