Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is possible to set default type for enum to be unsigned char?

Tags:

c++

I have to pack some enumerations to byte array (char*) and send over network. Is possible to set default type for enum to be unsigned char? (I can now cast or use & 0xff to extract first byte/char but that requires additional operations so is there any way to solve this at definition of enum?)

like image 425
PaolaJ. Avatar asked Dec 09 '12 16:12

PaolaJ.


People also ask

What is the default type of 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.

Are enum signed or unsigned?

An enum type is represented by an underlying integer type. The size of the integer type and whether it is signed is based on the range of values of the enumerated constants. In strict C89 or C99 mode, the compiler allows only enumeration constants with values that will fit in "int" or "unsigned int" (32 bits).

What is default enum type in C++?

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.

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


1 Answers

This is only possible with C++11 strongly typed enums:

enum class MyEnum : unsigned char { E1, E2 };

See here for more information

like image 165
juanchopanza Avatar answered Oct 13 '22 19:10

juanchopanza