Fix C++ enum conversion with fixed bool underlying type.
C++ enum conversion now correctly handles fixed `bool` underlying types per C++ standard.
A bug in GCC’s C++ type conversion has been fixed, resolving incorrect behavior when casting to an enum with a bool underlying type. The C++ standard requires that such operands first be converted to the underlying type (bool in this case) and then to the enum. Previously, GCC would directly convert to the enum type, which for bool underlying types truncated to a single bit, causing values like 2 to be misinterpreted as false. This patch ensures the conversion follows the standard by going through the bool type, correctly handling truth values and integrating with existing boolean warning diagnostics.
In Details
The ocp_convert function in GCC's C++ frontend has been updated to correctly handle static casts to enumerations with fixed underlying types, specifically addressing issues with bool as the underlying type ([expr.static.cast]/8). Previously, the conversion bypassed the intermediate step of casting to the underlying type, leading to incorrect results for bool underlying types due to the direct conversion to the enum's bit-level representation. The fix now routes these conversions through the appropriate boolean conversion path, ensuring consistent behavior and proper integration with rel…
- enum
- An enumerated type in C++. It defines a type consisting of a set of named integer constants. The underlying type can be explicitly specified (e.g.,
enum MyEnum : unsigned int). - underlying type
- The fundamental integral type used to represent the values of an enumeration. In C++, this can be explicitly specified or determined by the compiler.
- bool
- A fundamental C++ data type representing boolean values, typically
trueorfalse. In C++, integer types can be implicitly converted tobool. - [expr.static.cast]/8
- A reference to a specific clause in the C++ standard that details the rules for
static_castconversions, particularly concerning enumerations and their underlying types.