-
Notifications
You must be signed in to change notification settings - Fork 8
Description
Full name of submitter (unless configured in github; will be published with the issue): Hubert Tong
Reference (section label): [diff.expr]
Link to reflector thread (if any): N/A
Issue description:
The resolution for FR-030 in WG 14 N3148 clarifies that, in the usual arithmetic conversions, C23 converts values of an enumeration type to the underlying type of the enumeration type. For an unscoped enumeration type whose underlying type is not fixed, C++ instead performs integral promotions (https://wg21.link/conv.prom#3) that do not rely upon the implementation's choice of underlying type as part of the usual arithmetic conversions.
Note: I suspect the C committee might want to change their integral promotion rules for enumeration types instead of their change to their usual arithmetic conversions.
Suggested resolution:
Add a new entry.
Affected subclause: 7.4 [expr.arith.conv]
Change: The usual arithmetic conversions differ, between C and C++, in the treatment of enumeration types with no fixed underlying type. In C++, values of such types are not specified as converting to the underlying type of the enumeration as part of the usual arithmetic conversions. Instead, integral promotions ([conv.prom]) based on the values of the enumeration ([dcl.enum]) are applied.
Rationale: Avoids the difference that can arise in C when replacing an enumeration constant in an expression with a cast of the same enumeration constant to the enumeration type. Allows C++ to treat an enumerator with behavior like that of int even while giving the enumerator the type of its enumeration.
Effect on original feature: Changes to semantics of well-defined feature. Some C expressions that have a dependence upon the implementation-defined underlying type of affected enumeration types will yield different results (as they would if a different underlying type is chosen by the C implementation).
[Example:
typedef enum { E0 } E;
static_assert(((E)E0 - 1 < 0) == (E0 - 1 < 0), "Must pass for C++");The static_assert may fail in C. —end example]
Difficulty of converting: Programs must add explicit casts to the underlying type.
How widely used: Rare.