Confusion over enum type expressions

Moderators: david ward, misra cpp

Post Reply
gs
Posts: 87
Joined: Thu Nov 18, 2004 1:39 am

Confusion over enum type expressions

Post by gs » Thu Aug 21, 2008 7:57 pm

Hi,
The rationale for rule 7-2-1 states,
It is unspecified behaviour if the evaluation of an expression with enum underlying type yields a value which does not correspond to one of the enumerators of the enumeration.
This assertion contains a slight inaccuracy. The relevant section of the ISO C++ (2003) standard, 7.2(9), actually reads,
An expression of arithmetic or enumeration type can be converted to an enumeration type explicitly. The value is unchanged if it is in the range of enumeration values of the enumeration type; otherwise the resulting enumeration value is unspecified.
(emphasis added by this Poster).

Should the MISRA C++ rule, instead, read,
An expression with enum underlying type shall only have values within the range of the enumerators of the enumeration.
(emphasis, again, added by this Poster)?

misra cpp
Posts: 148
Joined: Mon Jun 02, 2008 1:55 pm
Company: MISRA

Re: Confusion over enum type expressions

Post by misra cpp » Mon Oct 05, 2015 3:05 pm

The rule is correct. The rationale for 7-5-2 states:
Additionally, other rules in this standard assume that objects of enum type only contain values corresponding to the enumerators. This rule ensures the validity of these assumptions.
Posted by and on behalf of
the MISRA C++ Working Group

Post Reply

Return to “6.7 Declarations (C++)”