Skip to content

When using magic_enum in combination with fmtlib, compilation errors occur with older versions of compilers. #441

@WtDMaO0

Description

@WtDMaO0

Hello everyone, I'm currently using magic_enum in combination with the fmtlib repository. In my current usage scenario, some machines still have very old compiler versions, specifically using gcc7. During usage, I discovered compilation errors occurring, with the issue located in the enum_integer module.

https://github.com/WtDMaO0/magic_enum/blob/2e6e8bfff361c2c40990426c462b44af6c27820b/include/magic_enum/magic_enum.hpp#L1270

// Returns integer value from enum value.
template <typename E>
[[nodiscard]] constexpr auto enum_integer(E value) noexcept -> detail::enable_if_t<E, underlying_type_t<E>> {
  return static_cast<underlying_type_t<E>>(value);
}

enum_interger is used here
https://github.com/WtDMaO0/magic_enum/blob/2e6e8bfff361c2c40990426c462b44af6c27820b/include/magic_enum/magic_enum_format.hpp#L55

template <typename E, std::enable_if_t<std::is_enum_v<std::decay_t<E>>, int> = 0>
std::string format_as(E e) {
  using D = std::decay_t<E>;
  static_assert(std::is_same_v<char, magic_enum::string_view::value_type>, "magic_enum::formatter requires string_view::value_type type same as char.");
  if constexpr (magic_enum::detail::supported<D>::value) {
    if constexpr (magic_enum::detail::subtype_v<D> == magic_enum::detail::enum_subtype::flags) {
      if (const auto name = magic_enum::enum_flags_name<D>(e); !name.empty()) {
        return {name.data(), name.size()};
      }
    } else {
      if (const auto name = magic_enum::enum_name<D>(e); !name.empty()) {
        return {name.data(), name.size()};
      }
    }
  }
  return std::to_string(magic_enum::enum_integer<D>(e));
}

As can be seen here, enum_integer is used inside format_as for scenarios not supported by magic_enum. However, the deduced type of enum_integer is detail::enable_if_t<E, underlying_type_t>, which triggers compiler compatibility checks and consequently causes the compilation error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions