How to find the range of enums ?
I have got two versions which state it differently when the lowest value is negative.Stroustrup says that for
enum e3 { min = -10 , max = 1000000 }; // range -1048576:1048575
but
C++ Primer Plus 5th edition says
for eg if the smallest enumerator is -6, the next power of two [times a minus sign] is -8, and thus the lower limit is -7
I am confused which is correct ?
I believe both are correct (see below for Primer’s definition, though), depending on how compatible you want to are. The formal definition is
For negative numbers, the question is what representation we use. The footnote to it says
If you assume sign magnitude or one’s complement then the example enumeration’s range is
-1048575:1048575. For two’s complement you get one more in the negative range. Primer’s definiton lacks the maximum enumerator value, so I’m not sure how it comes to lower limit-7. If you want to be maximum compatible with other implementations, I would go with-1048575:1048575.