Enumeration types can be used in a variety of ways, but the MISRA-C working group decided that there were two distinct uses.
1. The members of the enumerated type form a set distinct from another enumeration type, such that an object of that type should not be assigned a value of a different enumeration type or to any other type. This allows the extension of MISRA strict typing to enumerated types. A cast must be used if the integer value of the enumeration constant is required.
2. The members of the enumerated type form a set of integer constants which are used in essentially integer
contexts. No cast is required if the integer value of the enumeration constant is required.
The rationale was that if you could create an object with an enumerated type, then that object should have a type distinct from an object with a different enumeration type. This is the MISRA named enumeration type
. However it was recognised that a common usage case was to use enumeration types as a way of holding a set of integer constants and that it would be annoying to be forced to write a cast each time such a constant was used. This is the MISRA anonymous enumeration type.
The C standard
does not give a way of distinguishing between these uses and therefore the MISRA-C working group imposed a convention on the C syntax. The presence of a tag
in the enum declaration would enable an object to be declared with that type some-where else in the code. Removing the tag means that only the enumeration constants can be used and it was for this reason that the MISRA-C working group restricted this syntactic form to be an anonymous enumeration type
Note: An object which is declared at the same time as an enumeration type with no tag is considered to be a named enumeration type