Title
Usual arithmetic conversions for enumerations are different in C
Status
open
Section
C.7.4 [diff.expr]
Submitter
Hubert Tong

Created on 2025-12-09.00:00:00 last changed 1 month ago

Messages

Date: 2025-12-21.21:49:29

Suggested resolution:

Add a new entry in C.7.4 [diff.expr] as follows:

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 (7.3.7 [conv.prom]) based on the values of the enumeration (9.8.1 [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.
Date: 2025-12-09.00:00:00

(From submission #829.)

C23 converts values of an enumeration type to the underlying type of the enumeration type (which might be different from int), but enumerators always have type int. In contrast, in C++ enumerators have the type of their enumeration and integral promotions are performed, which are independent of the implementation's choice of underlying type.

History
Date User Action Args
2025-12-21 21:49:29adminsetmessages: + msg8448
2025-12-09 00:00:00admincreate