improve nameof_enum

This commit is contained in:
terik23 2019-04-28 14:30:09 +05:00
parent 07696f969a
commit 1a38e075d0

View file

@ -90,25 +90,22 @@ template <typename E, E V>
[[nodiscard]] constexpr std::string_view enum_name_impl() noexcept { [[nodiscard]] constexpr std::string_view enum_name_impl() noexcept {
static_assert(std::is_enum_v<E>, "nameof::enum_name_impl requires enum type."); static_assert(std::is_enum_v<E>, "nameof::enum_name_impl requires enum type.");
#if defined(__clang__) #if defined(__clang__)
constexpr std::string_view name{__PRETTY_FUNCTION__}; constexpr std::string_view name{__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2};
constexpr auto suffix = sizeof("]") - 1;
#elif defined(__GNUC__) && __GNUC__ >= 9 #elif defined(__GNUC__) && __GNUC__ >= 9
constexpr std::string_view name{__PRETTY_FUNCTION__}; constexpr std::string_view name{__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 51};
constexpr auto suffix = sizeof("; std::string_view = std::basic_string_view<char>]") - 1;
#elif defined(_MSC_VER) #elif defined(_MSC_VER)
constexpr std::string_view name{__FUNCSIG__}; constexpr std::string_view name{__FUNCSIG__, sizeof(__FUNCSIG__) - 17};
constexpr auto suffix = sizeof(">(void) noexcept") - 1;
#else #else
return {}; // Unsupported compiler. return {}; // Unsupported compiler.
#endif #endif
#if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 9) || defined(_MSC_VER) #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') { if constexpr (name[prefix] >= '0' && name[prefix] <= '9') {
return {}; // Value does not have name. return {}; // Value does not have name.
} else { } else {
return name.substr(prefix, name.length() - prefix - suffix); return name.substr(prefix, name.length() - prefix);
} }
#endif #endif
} }