Skip to content

Iterate over empty enum #451

@Rayman

Description

@Rayman

I want to iterate over the values in an enum, but these enums could be empty, see the example below. Is this a bug inside magic_enum or should I guard against these enums with enum_count?

#include <iostream>
#include <magic_enum/magic_enum.hpp>

enum class EmptyEnum {
};

void test() {
    for (const auto& value : magic_enum::enum_values<EmptyEnum>()) {
        std::cout << std::to_underlying(value) << std::endl;
    }
}

Currently I'm getting this compilation error:

/opt/compiler-explorer/libs/magic_enum/trunk/include/magic_enum/magic_enum.hpp: In instantiation of 'constexpr magic_enum::detail::enable_if_t<E, magic_enum::detail::values_t<E, S> > magic_enum::enum_values() [with E = EmptyEnum; detail::enum_subtype S = magic_enum::detail::enum_subtype::common; detail::enable_if_t<E, detail::values_t<E, S> > = const std::array<EmptyEnum, 0>&; detail::values_t<E, S> = const std::array<EmptyEnum, 0>&; typename std::decay<_Tp>::type = EmptyEnum]':
<source>:8:64:   required from here
    8 |     for (const auto& value : magic_enum::enum_values<EmptyEnum>()) {
      |                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
/opt/compiler-explorer/libs/magic_enum/trunk/include/magic_enum/magic_enum.hpp:1248:25: error: static assertion failed: magic_enum requires enum implementation and valid max and min.
 1248 |   static_assert(detail::is_reflected_v<D, S>, "magic_enum requires enum implementation and valid max and min.");
      |                 ~~~~~~~~^~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/libs/magic_enum/trunk/include/magic_enum/magic_enum.hpp:1248:25: note: 'magic_enum::detail::is_reflected_v<EmptyEnum, magic_enum::detail::enum_subtype::common>' evaluates to false
Compiler returned: 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions