0

I have learnt that enums are data types as opposed to data structures. I have also learnt that usually they are nominal in nature rather than ordinal. Consequentially, it does not make sense to iterate through an enum, or obtain the enum's constant value like you would an array, e.g. week[3].

However, I have come across instances where an index is used to obtain the value from an enum:

#include <iostream>

enum week {Mon=5, Tues, Wed};

int main()
{
    week day = (week)0;
    std::cout << day << "\n"; // outputs 0
    day = (week)13;
    std::cout << day << "\n"; // outputs 13
    return 0;
}

How is this working?

I assumed (week)13 would not work, given there is nothing at this index. Is it that the cast is failing and falling back to the type to be cast?

As a side note: I believe some confusion with this style of code in C/C++ may occur due to other languages (C#) handling this case differently.

Edit: The linked solutions don't answer the question I'm asking. 1 is about comparing integers to enums -- in this case I'm asking why casting an int to an enum gives a certain result.[2] (Assigning an int value to enum and vice versa in C++) mentions "Old enums (unscoped enums) can be converted to integer but the other way is not possible (implicitly). ", but by the accounts of my code, it does seem possible. Lastly 3 has nothing to do with this question: I'm not talking about assignment of enums, rather the casting of them via an integer.

Zeteroo
  • 33
  • 1
  • 4
  • C# handles that case the same way, by the way. – Etienne de Martel Jan 20 '22 at 00:49
  • Reopened. None of the claimed duplicates addressed the question here, where the value being assigned is not equal to the value of any of the enumerators. – Pete Becker Jan 20 '22 at 01:55
  • 1
    It is explained in https://en.cppreference.com/w/cpp/language/enum -- the enum has an *underlying type* which is an integer type that can hold all of the enumerators, and you are allowed to use any value of that type, even if it doesn't correspond to an enumerator. It's fairly common to use enumerators as bit flags, e.g. set values for 1,2,4,8 and allow 13 as a bitwise-OR combination of three flags – M.M Jan 20 '22 at 02:06
  • @M.M ok, but what is it doing then in this circumstance : (week)13 ? How is it actually returning 13? Is it looking into the enum seeing the number does not exist then returning it regardless because the enum can use any value? – Zeteroo Jan 20 '22 at 04:14

1 Answers1

1

The rule, roughly, is that any value hat fits in the bits needed to represent all of the enumerators can be held in an enumerated type. In the example in the question, Wed has the value 7, which requires three bits. So any value from 0 to 7 can be stored in an object of type week. Even if Wed wasn’t there, the remaining two values would need the same three low bits, so the values 0 to 7 would all still work. 13, on the other hand requires the fourth bit, which isn’t needed for any of the enumerators, so is not required to work.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • Ok this makes sense. I guess then it's compiler specific as to how to implement exceptions made when there are more bits requested than the enum holds. As to where that is defined in gcc...I guess that's an investigation for another day. Thanks. – Zeteroo Jan 21 '22 at 00:02