magic_enum: Breakage in Clang trunk
Clang has recently addressed an https://github.com/llvm/llvm-project/issues/50055 where it failed to diagnose the UB of casting an out-of-range integer to an enum in a constant expression.
Since undefined behavior is not allowed in a constant expression, Clang now prohibits enum test { zero }; constexpr int g = static_cast<test>(-1);
with error: constexpr variable 'g' must be initialized by a constant expression; note: integer value -1 is outside the valid range of values [0, 1] for this enumeration type
This change breaks magic_enum library, since the library internally uses the [-128, 128]
default enum value range. Demonstrated with example, below
#include "magic_enum.hpp"
enum Color { Red, Black };
int main(int argc, char* argv[]) {
Color color = Red;
magic_enum::enum_name(color);
return 0;
}
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 19 (15 by maintainers)
We plan on making this a hard error in the next release. If you are using some internal enum type then giving a fixed underlying type should fix things e.g.
If you are using the enum type from the user internally, perhaps you can switch to using some new internal enum type that has a fixed underlying type?
Nice workaround!
This issue is linked already to the llvm-project bug.
Below is clang diagnostics: