Wednesday, 11 September 2013

Does the actual value of a enum class enumeration remain constant/invariant?

Does the actual value of a enum class enumeration remain constant/invariant?

Given code for an incomplete server like:
enum class Command : uint32_t {
LOGIN,
MESSAGE,
JOIN_CHANNEL,
PART_CHANNEL,
INVALID
};
Can I expect that converting Command::LOGIN to an integer will always give
the same value?
Across compilers?
Across compiler versions?
If I add another enumeration?
If I remove an enumeration?
Converting Command::LOGIN would look something like this:
uint32_t number = static_cast<uint32_t>(Command::LOGIN);
Some extra information on what I am doing here. This enumeration is fed
onto the wire by converting it to an integer sending it along to the
server/client. I do not really particularly care what the number is, as
long as it will always stay the same. If it will not stay the same, then
obviously I will have to provide my own numbers through the usual way.
Now my sneaking suspicion is that it will change depending on what
compiler was used to compile the code, but I would like to know for sure.
Bonus question: How does the compiler/language determine what number to
use for Command::LOGIN?
Before submitting this question, I have noticed some changes from say
3137527848 to 0 and back, so it is obviously not valid to rely on it not
changing. I am still curious about how this number is determined, and how
or why that number is changing.

No comments:

Post a Comment