diff --git a/include/magic_enum/magic_enum_format.hpp b/include/magic_enum/magic_enum_format.hpp index 15bf43c80..974ac2c6a 100644 --- a/include/magic_enum/magic_enum_format.hpp +++ b/include/magic_enum/magic_enum_format.hpp @@ -35,10 +35,12 @@ #include "magic_enum.hpp" #include "magic_enum_flags.hpp" +namespace magic_enum::detail { + template >, int> = 0> std::string format_as(E e) { using D = std::decay_t; - + static_assert(std::is_same_v, "magic_enum::formatter requires string_view::value_type type same as char."); if constexpr (magic_enum::detail::supported::value) { if constexpr (magic_enum::detail::subtype_v == magic_enum::detail::enum_subtype::flags) { if (const auto name = magic_enum::enum_flags_name(e); !name.empty()) { @@ -53,17 +55,15 @@ std::string format_as(E e) { return std::to_string(magic_enum::enum_integer(e)); } -#if defined(__cpp_lib_format) +} // namespace magic_enum::format -#ifndef MAGIC_ENUM_USE_STD_MODULE -#include -#endif +#if defined(__cpp_lib_format) template struct std::formatter>, char>> : std::formatter { template auto format(E e, FormatContext& ctx) const { - return formatter::format(format_as(e), ctx); + return formatter::format(magic_enum::detail::format_as(e), ctx); } }; @@ -75,7 +75,7 @@ template struct fmt::formatter>, char>> : fmt::formatter { template auto format(E e, FormatContext& ctx) const { - return formatter::format(format_as(e), ctx); + return formatter::format(magic_enum::detail::format_as(e), ctx); } }; diff --git a/test/test.cpp b/test/test.cpp index ef0459dae..9d26eab8b 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -1176,6 +1176,7 @@ TEST_CASE("multdimensional-switch-case") { #if defined(__cpp_lib_format) +#include #include TEST_CASE("format-base") {