From 6fca52c1e72dd53cc8dc770bd8b7e3ece358f2d5 Mon Sep 17 00:00:00 2001 From: neargye Date: Mon, 7 Nov 2022 21:10:41 +0400 Subject: [PATCH] compile-time optimization --- include/magic_enum.hpp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/include/magic_enum.hpp b/include/magic_enum.hpp index fa17dde..517f7fc 100644 --- a/include/magic_enum.hpp +++ b/include/magic_enum.hpp @@ -398,15 +398,15 @@ constexpr auto n() noexcept { #else constexpr auto name = string_view{}; #endif - return static_string{name}; + return name; } else { - return static_string<0>{}; // Unsupported compiler or Invalid customize. + return string_view{}; // Unsupported compiler or Invalid customize. } } template constexpr auto type_name() noexcept { - static_assert(is_enum_v, "magic_enum::detail::n requires enum type."); + static_assert(is_enum_v, "magic_enum::detail::type_name requires enum type."); [[maybe_unused]] constexpr auto custom = customize::enum_type_name(); static_assert(std::is_same_v, customize::customize_t>, "magic_enum::customize requires customize_t type."); @@ -415,7 +415,8 @@ constexpr auto type_name() noexcept { static_assert(!name.empty(), "magic_enum::customize requires not empty string."); return static_string{name}; } else { - return n(); + constexpr auto name = n(); + return static_string{name}; } } @@ -434,15 +435,15 @@ constexpr auto n() noexcept { #else constexpr auto name = string_view{}; #endif - return static_string{name}; + return name; } else { - return static_string<0>{}; // Unsupported compiler or Invalid customize. + return string_view{}; // Unsupported compiler or Invalid customize. } } template constexpr auto enum_name() noexcept { - static_assert(is_enum_v, "magic_enum::detail::n requires enum type."); + static_assert(is_enum_v, "magic_enum::detail::enum_name requires enum type."); [[maybe_unused]] constexpr auto custom = customize::enum_name(V); static_assert(std::is_same_v, customize::customize_t>, "magic_enum::customize requires customize_t type."); @@ -451,7 +452,8 @@ constexpr auto enum_name() noexcept { static_assert(!name.empty(), "magic_enum::customize requires not empty string."); return static_string{name}; } else { - return n(); + constexpr auto name = n(); + return static_string{name}; } } @@ -465,6 +467,9 @@ constexpr bool is_valid() noexcept { #if defined(__clang__) && __clang_major__ >= 16 // https://reviews.llvm.org/D130058, https://reviews.llvm.org/D131307 constexpr E v = __builtin_bit_cast(E, V); +#else + constexpr E v = static_cast(V); +#endif [[maybe_unused]] constexpr auto custom = customize::enum_name(v); static_assert(std::is_same_v, customize::customize_t>, "magic_enum::customize requires customize_t type."); if constexpr (std::is_same_v, customize::customize_t> && custom.index() == 0) { @@ -474,9 +479,6 @@ constexpr bool is_valid() noexcept { } else { return n().size() != 0; } -#else - return enum_name(V)>().size() != 0; -#endif } template >