SanderMertens / flecs

A fast entity component system (ECS) for C & C++
https://www.flecs.dev
Other
6.46k stars 454 forks source link

clang-20 - compilation error 😢 #1311

Closed alexv-ds closed 1 month ago

alexv-ds commented 3 months ago

Describe the bug Compilation error

addons/cpp/utils/enum.hpp:240:33: error: no matching function for call to 'to_int'

To Reproduce Steps to reproduce the behavior:

  1. Get sample.zip
  2. Compile it

Additional context tested on - clang-20, ubuntu-24.04

apache-hb commented 1 month ago

I've also hit this and have a bit more context as to why this has become an issue.

to_int casts an integer to an enum value, clang incorrectly used to allow any integer value to be cast to an enum value at compile time, since 20 clang now refuses to instantiate enum values at compile time with a value not in the representable bit range of enum values. C++ (since 20?) specifies that enums are at least as large as the minimum number of bits required to represent all values in 2s complement.

This means its no longer enough to use FLECS_ENUM_MAX(E) as an upper bound, some other way will need to be used to prevent going out of range. std::numeric_limits<std::underlying_type_t<E>> won't be enough because it'll round up to the nearest 8 bits, and even going 1 bit out of range triggers this compiler error.

SanderMertens commented 1 month ago

magic_enum ran into the same issue, this looks like a potential fix: https://github.com/Neargye/magic_enum/blob/master/include/magic_enum/magic_enum.hpp#L626

SanderMertens commented 1 month ago

Fixed!