Neargye / magic_enum

Static reflection for enums (to string, from string, iteration) for modern C++, work with any enum type without any macro or boilerplate code
MIT License
4.76k stars 422 forks source link

Cannot use `std::to_underlying` in `enum_for_each` if the value is declared via auto deduced type #347

Closed GTruf closed 4 months ago

GTruf commented 4 months ago

Compiler: GCC 13.1.0 OS: Ubuntu 22.04 CPP Standard: 23

Code:

enum class some_enum_e : uint8_t
{
    CREATE,
    DELETE
};

// ...

magic_enum::enum_for_each<some_enum_e>([&](auto value)
{
    auto value1 = value; // Ok
    auto value2 = std::to_underlying(value); // Compiler error

    // Because of this we have to either abandon auto or specify the enum type in angle brackets in std::to_underlying
    auto value3 = std::to_underlying<some_enum_e>(value); // Ok
});

Compiler error is:

...
error: no type named 'type' in 'struct std::underlying_type<std::integral_constant<some_enum_e, some_enum_e::DELETE> >'
 2622 |     using underlying_type_t = typename underlying_type<_Tp>::type;
Neargye commented 4 months ago

value have type std::integral_constant<some_enum_e, some_enum_e::DELETE>, try

auto value2 = std::to_underlying(value());

or if don't need compiletime use

for (auto v : magic_enun::values<some_enum_e>())
  auto value2 = std::to_underlying(value);
GTruf commented 4 months ago

Thank you for your reply