From 47fa9b5f7c32cf1aa16d46dba0d071575ebd81b6 Mon Sep 17 00:00:00 2001 From: neargye Date: Tue, 1 Oct 2019 18:55:37 +0500 Subject: [PATCH] fix enums --- include/nameof.hpp | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/include/nameof.hpp b/include/nameof.hpp index 30f11b3..ceaec05 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -235,6 +235,9 @@ using remove_cvref_t = std::remove_cv_t>; template using enable_if_enum_t = std::enable_if_t, R>; +template +inline constexpr bool is_enum_v = std::is_enum_v && std::is_same_v>; + constexpr std::string_view pretty_name(std::string_view name, bool remove_template_suffix = true) noexcept { if (name.size() >= 1 && (name[0] == '"' || name[0] == '\'')) { return {}; // Narrow multibyte string literal. @@ -316,7 +319,7 @@ constexpr std::string_view pretty_name(std::string_view name, bool remove_templa template constexpr auto n() noexcept { - static_assert(std::is_enum_v, "nameof::detail::n requires enum type."); + static_assert(is_enum_v, "nameof::detail::n requires enum type."); #if defined(__clang__) || defined(__GNUC__) constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2}); #elif defined(_MSC_VER) @@ -336,9 +339,6 @@ inline constexpr auto enum_name_v = n(); namespace enums { -template -inline constexpr bool is_enum_v = std::is_enum_v && std::is_same_v>; - template inline constexpr int reflected_min_v = static_cast(enum_range::min > (std::numeric_limits>::min)() ? enum_range::min @@ -472,30 +472,30 @@ constexpr auto strings() noexcept { } template -struct enum_traits { - static constexpr std::string_view name(E value) noexcept { - if (static_cast(value) >= static_cast(min_v) && static_cast(value) <= static_cast(max_v)) { - if constexpr (sparsity_v) { - if (auto i = indexes[static_cast(value) - min_v]; i != invalid_index_v) { - return strings[i]; - } - } else { - return strings[static_cast(value) - min_v]; - } - } - - return {}; // Value out of range. - } - - private: +class enum_traits final { static_assert(is_enum_v, "nameof::enum_traits requires enum type."); static_assert(enum_range::min > (std::numeric_limits::min)(), "nameof::enum_range requires min must be greater than INT16_MIN."); static_assert(enum_range::max < (std::numeric_limits::max)(), "nameof::enum_range requires max must be less than INT16_MAX."); static_assert(enum_range::max > enum_range::min, "nameof::enum_range requires max > min."); static_assert(count_v > 0, "nameof::enum_range requires enum implementation or valid max and min."); using U = std::underlying_type_t; - inline static constexpr auto strings = strings(); - inline static constexpr auto indexes = indexes(range_v); + inline static constexpr auto strings_ = strings(); + inline static constexpr auto indexes_ = indexes(range_v); + + public: + static constexpr std::string_view name(E value) noexcept { + if (static_cast(value) >= static_cast(min_v) && static_cast(value) <= static_cast(max_v)) { + if constexpr (sparsity_v) { + if (auto i = indexes_[static_cast(value) - min_v]; i != invalid_index_v) { + return strings_[i]; + } + } else { + return strings_[static_cast(value) - min_v]; + } + } + + return {}; // Value out of range. + } }; } // namespace nameof::detail::enums