From 6e97229efd6ae2eae0c3dfc3dc01923bc47c1335 Mon Sep 17 00:00:00 2001 From: neargye Date: Fri, 14 Feb 2020 15:28:16 +0500 Subject: [PATCH] more friendly static checks --- include/nameof.hpp | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/include/nameof.hpp b/include/nameof.hpp index e4887de..fe4a90d 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -51,13 +51,13 @@ // Checks nameof_type compiler compatibility. #if defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER) -# undef NAMEOF_TYPE_SUPPORTED +# undef NAMEOF_TYPE_SUPPORTED # define NAMEOF_TYPE_SUPPORTED 1 #endif // Checks nameof_enum compiler compatibility. #if defined(__clang__) || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) -# undef NAMEOF_ENUM_SUPPORTED +# undef NAMEOF_ENUM_SUPPORTED # define NAMEOF_ENUM_SUPPORTED 1 #endif @@ -378,7 +378,6 @@ constexpr auto n() noexcept { return std::string_view{}; } #else - static_assert(nameof_enum_supported::value, "nameof::nameof_enum: Unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); return std::string_view{}; // Unsupported compiler. #endif } @@ -527,7 +526,7 @@ constexpr auto strings() noexcept { template class enum_traits { static_assert(is_enum_v, "nameof::enum_traits requires enum type."); - static_assert(count_v > 0, "nameof::enum_range requires enum implementation or valid max and min."); + static_assert(count_v > 0, "nameof::enum_range requires enum implementation and valid max and min."); using U = std::underlying_type_t; inline static constexpr auto strings_ = strings(); inline static constexpr auto indexes_ = indexes(std::make_integer_sequence>{}); @@ -560,11 +559,9 @@ constexpr auto n() noexcept { # elif defined(_MSC_VER) constexpr std::string_view name{__FUNCSIG__ + 63, sizeof(__FUNCSIG__) - 81 - (__FUNCSIG__[sizeof(__FUNCSIG__) - 19] == ' ' ? 1 : 0)}; # endif - static_assert(name.size() > 0, "Type does not have a name."); return cstring{name}; #else - static_assert(nameof_type_supported::value, "nameof::nameof_type: Unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); return std::string_view{}; // Unsupported compiler. #endif } @@ -583,6 +580,7 @@ inline constexpr bool is_nameof_enum_supported = detail::nameof_enum_supported [[nodiscard]] constexpr auto nameof_enum(E value) noexcept -> detail::enable_if_enum_t { + static_assert(detail::nameof_enum_supported::value, "nameof::nameof_enum unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); return detail::enums::enum_traits>::name(value); } @@ -590,7 +588,9 @@ template // This version is much lighter on the compile times and is not restricted to the enum_range limitation. template [[nodiscard]] constexpr auto nameof_enum() noexcept -> detail::enable_if_enum_t { - constexpr std::string_view name = detail::enum_name_v, V>; + using E = detail::remove_cvref_t; + static_assert(detail::nameof_enum_supported::value, "nameof::nameof_enum unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); + constexpr std::string_view name = detail::enum_name_v; static_assert(name.size() > 0, "Enum value does not have a name."); return name; @@ -599,21 +599,31 @@ template // Obtains string name of type, reference and cv-qualifiers are ignored. template [[nodiscard]] constexpr std::string_view nameof_type() noexcept { + static_assert(detail::nameof_type_supported::value, "nameof::nameof_type unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); #if defined(_MSC_VER) - return detail::type_name_v>>; + using U = detail::identity>; #else - return detail::type_name_v>; + using U = detail::remove_cvref_t; #endif + constexpr std::string_view name = detail::type_name_v; + static_assert(name.size() > 0, "Type does not have a name."); + + return name; } // Obtains string name of full type, with reference and cv-qualifiers. template [[nodiscard]] constexpr std::string_view nameof_full_type() noexcept { + static_assert(detail::nameof_type_supported::value, "nameof::nameof_type unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); #if defined(_MSC_VER) - return detail::type_name_v>; + using U = detail::identity; #else - return detail::type_name_v; + using U = T; #endif + constexpr std::string_view name = detail::type_name_v; + static_assert(name.size() > 0, "Type does not have a name."); + + return name; } } // namespace nameof