Are C++ enums signed or unsigned?

Let's go to the source. Here's what the C++03 standard (ISO/IEC 14882:2003) document says in 7.2-5 (Enumeration declarations):

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.

In short, your compiler gets to choose (obviously, if you have negative numbers for some of your ennumeration values, it'll be signed).


You shouldn't depend on them being signed or unsigned. If you want to make them explicitly signed or unsigned, you can use the following:

enum X : signed int { ... };    // signed enum
enum Y : unsigned int { ... };  // unsigned enum

You shouldn't rely on any specific representation. Read the following link. Also, the standard says that it is implementation-defined which integral type is used as the underlying type for an enum, except that it shall not be larger than int, unless some value cannot fit into int or an unsigned int.

In short: you cannot rely on an enum being either signed or unsigned.

Tags:

C++

Enums