This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 110b. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2023-01-15


1094. Converting floating-point values to scoped enumeration types

Section: 7.6.1.9  [expr.static.cast]     Status: C++11     Submitter: Daniel Krügler     Date: 2010-07-17

[Voted into the WP at the March, 2011 meeting.]

According to 9.7.1 [dcl.enum] paragraph 10,

An expression of arithmetic or enumeration type can be converted to an enumeration type explicitly.

However, 7.6.1.9 [expr.static.cast] paragraph 10 says only,

A value of integral or enumeration type can be explicitly converted to an enumeration type.

This omits floating-point values. Presumably unscoped enumeration types are covered by paragraph 7,

The inverse of any standard conversion sequence ( 7.3 [conv]), other than the lvalue-to-rvalue (7.3.2 [conv.lval]), array-to- pointer (7.3.3 [conv.array]), function-to-pointer (7.3.4 [conv.func]), and boolean (7.3.14 [conv.fctptr]) conversions, can be performed explicitly using static_cast.

because 7.3.11 [conv.fpint] paragraph 2 allows an unscoped enumeration value to be implicitly converted to a floating point type. (Although that also covers the integral types, so it's not clear why they would be mentioned specifically in 7.6.1.9 [expr.static.cast] paragraph 10.) However, this should presumably say “arithmetic” instead of “integral” to match the statement in 9.7.1 [dcl.enum] paragraph 10.

Proposed resolution (November, 2010):

  1. Change 7.6.1.9 [expr.static.cast] paragraph 10 as follows:

  2. A value of integral or enumeration type can be explicitly converted to an enumeration type. The value is unchanged if the original value is within the range of the enumeration values (9.7.1 [dcl.enum]). Otherwise, the resulting enumeration value is unspecified (and might not be in that range). A value of floating-point type can also be converted to an enumeration type. The resulting value is the same as converting the original value to the underlying type of the enumeration (7.3.11 [conv.fpint]), and subsequently to the enumeration type.
  3. Add the following footnote to the end of 9.7.1 [dcl.enum] paragraph 7:

  4. ...If the enumerator-list is empty, the values of the enumeration are as if the enumeration had a single enumerator with value 0. [Footnote: This set of values is used to define promotion and conversion semantics for the enumeration type; it does not exclude an expression of enumeration type from having a value that falls outside this range. —end footnote]
  5. Delete 9.7.1 [dcl.enum] paragraph 10:

  6. 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.