From 07181c5a11b06a39c5026fa8ace9329550539a6f Mon Sep 17 00:00:00 2001 From: Bela Schaum Date: Fri, 18 Feb 2022 16:52:09 +0100 Subject: [PATCH] support gcc8 with defined MAGIC_ENUM_NO_CHECK_SUPPORT and with specialized magic_enum::customize::enum_name (#137) --- include/magic_enum.hpp | 61 +++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/include/magic_enum.hpp b/include/magic_enum.hpp index 4118e6b..1174591 100644 --- a/include/magic_enum.hpp +++ b/include/magic_enum.hpp @@ -330,16 +330,19 @@ inline constexpr bool is_enum_v = std::is_enum_v && std::is_same_v constexpr auto n() noexcept { static_assert(is_enum_v, "magic_enum::detail::n requires enum type."); -#if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED -# if defined(__clang__) || defined(__GNUC__) - constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2}); -# elif defined(_MSC_VER) - constexpr auto name = pretty_name({__FUNCSIG__, sizeof(__FUNCSIG__) - 17}); -# endif - return static_string{name}; + + if constexpr (supported::value) { +#if defined(__clang__) || defined(__GNUC__) + constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2}); +#elif defined(_MSC_VER) + constexpr auto name = pretty_name({__FUNCSIG__, sizeof(__FUNCSIG__) - 17}); #else - return string_view{}; // Unsupported compiler. + constexpr auto name = string_view{}; #endif + return static_string{name}; + } else { + return string_view{}; // Unsupported compiler. + } } template @@ -348,20 +351,21 @@ inline constexpr auto type_name_v = n(); template constexpr auto n() noexcept { static_assert(is_enum_v, "magic_enum::detail::n requires enum type."); - constexpr auto custom_name = customize::enum_name(V); + [[maybe_unused]] constexpr auto custom_name = customize::enum_name(V); if constexpr (custom_name.empty()) { - static_cast(custom_name); -#if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED -# if defined(__clang__) || defined(__GNUC__) - constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2}); -# elif defined(_MSC_VER) - constexpr auto name = pretty_name({__FUNCSIG__, sizeof(__FUNCSIG__) - 17}); -# endif - return static_string{name}; + if constexpr (supported::value) { +#if defined(__clang__) || defined(__GNUC__) + constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2}); +#elif defined(_MSC_VER) + constexpr auto name = pretty_name({__FUNCSIG__, sizeof(__FUNCSIG__) - 17}); #else - return string_view{}; // Unsupported compiler. + constexpr auto name = string_view{}; #endif + return static_string{name}; + } else { + return string_view{}; // Unsupported compiler. + } } else { return static_string{custom_name}; } @@ -510,7 +514,7 @@ template inline constexpr bool is_flags_v = is_flags_enum(); template -inline constexpr auto values_v = values>(); +inline constexpr std::array values_v = values>(); template > using values_t = decltype((values_v)); @@ -532,7 +536,7 @@ constexpr auto names(std::index_sequence) noexcept { } template -inline constexpr auto names_v = names(std::make_index_sequence>{}); +inline constexpr std::array names_v = names(std::make_index_sequence>{}); template > using names_t = decltype((names_v)); @@ -545,7 +549,7 @@ constexpr auto entries(std::index_sequence) noexcept { } template -inline constexpr auto entries_v = entries(std::make_index_sequence>{}); +inline constexpr std::array entries_v = entries(std::make_index_sequence>{}); template > using entries_t = decltype((entries_v)); @@ -871,7 +875,7 @@ template } else { const auto v = static_cast(value); if (v >= detail::min_v && v <= detail::max_v) { - return static_cast(v - detail::min_v); + return static_cast(v - detail::min_v); } } @@ -947,14 +951,15 @@ template & operator<<(std::basic_ostream& os, E value) { using D = std::decay_t; using U = underlying_type_t; -#if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED - if (const auto name = magic_enum::enum_flags_name(value); !name.empty()) { - for (const auto c : name) { - os.put(c); + + if constexpr (detail::supported::value) { + if (const auto name = magic_enum::enum_flags_name(value); !name.empty()) { + for (const auto c : name) { + os.put(c); + } + return os; } - return os; } -#endif return (os << static_cast(value)); }