From 1a38e075d0656e94da3e796eee2dd79059c6111c Mon Sep 17 00:00:00 2001 From: terik23 Date: Sun, 28 Apr 2019 14:30:09 +0500 Subject: [PATCH] improve nameof_enum --- include/nameof.hpp | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/include/nameof.hpp b/include/nameof.hpp index e5395ce..210a8b2 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -90,25 +90,22 @@ template [[nodiscard]] constexpr std::string_view enum_name_impl() noexcept { static_assert(std::is_enum_v, "nameof::enum_name_impl requires enum type."); #if defined(__clang__) - constexpr std::string_view name{__PRETTY_FUNCTION__}; - constexpr auto suffix = sizeof("]") - 1; + constexpr std::string_view name{__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2}; #elif defined(__GNUC__) && __GNUC__ >= 9 - constexpr std::string_view name{__PRETTY_FUNCTION__}; - constexpr auto suffix = sizeof("; std::string_view = std::basic_string_view]") - 1; + constexpr std::string_view name{__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 51}; #elif defined(_MSC_VER) - constexpr std::string_view name{__FUNCSIG__}; - constexpr auto suffix = sizeof(">(void) noexcept") - 1; + constexpr std::string_view name{__FUNCSIG__, sizeof(__FUNCSIG__) - 17}; #else return {}; // Unsupported compiler. #endif #if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 9) || defined(_MSC_VER) - constexpr auto prefix = name.find_last_of(" :,-)", name.length() - suffix) + 1; + constexpr auto prefix = name.find_last_of(" :,-)") + 1; if constexpr (name[prefix] >= '0' && name[prefix] <= '9') { return {}; // Value does not have name. } else { - return name.substr(prefix, name.length() - prefix - suffix); + return name.substr(prefix, name.length() - prefix); } #endif }