c++enumssizeof

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


I have to pack some enumerations to byte array (char*) and send over network. Is it possible to set the 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?)


Solution

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

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

    See here for more information