From 9de7bbaeb8fd1ab6bcab92c37fc375d45c7bbda5 Mon Sep 17 00:00:00 2001 From: neargye Date: Thu, 12 Sep 2019 14:41:16 +0500 Subject: [PATCH] more sfinae-friendly --- include/nameof.hpp | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/include/nameof.hpp b/include/nameof.hpp index e68d20e..a95b1cd 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -83,7 +83,7 @@ struct identity final { using type = T; }; -template +template struct nameof_type_supported final #if defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER) || defined(NAMEOF_TYPE_NO_CHECK_SUPPORT) : std::true_type {}; @@ -123,7 +123,7 @@ template using remove_cvref_t = std::remove_cv_t>; template -using enable_if_enum_t = std::enable_if_t>, R>; +using enable_if_enum_t = std::enable_if_t, R>; template [[nodiscard]] constexpr std::string_view nameof(std::string_view name, bool remove_template_suffix = true) noexcept { @@ -221,6 +221,7 @@ template #elif defined(_MSC_VER) constexpr auto name = nameof({__FUNCSIG__, sizeof(__FUNCSIG__) - 17}); #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 @@ -248,6 +249,7 @@ template #elif defined(_MSC_VER) constexpr std::string_view name{__FUNCSIG__ + 63, sizeof(__FUNCSIG__) - 81 - (__FUNCSIG__[sizeof(__FUNCSIG__) - 19] == ' ' ? 1 : 0)}; #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 @@ -273,10 +275,8 @@ inline constexpr bool is_nameof_enum_supported = detail::nameof_enum_supported -[[nodiscard]] constexpr detail::enable_if_enum_t nameof_enum(E value) noexcept { +[[nodiscard]] constexpr auto nameof_enum(E value) noexcept -> detail::enable_if_enum_t { using D = detail::remove_cvref_t; - static_assert(detail::nameof_enum_supported::value, "nameof::nameof_enum: Unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); - static_assert(std::is_enum_v, "nameof::nameof_enum requires enum type."); static_assert(enum_range::min > (std::numeric_limits::min)(), "nameof::enum_range requires min must be greater than INT_MIN."); static_assert(enum_range::max < (std::numeric_limits::max)(), "nameof::enum_range requires max must be less than INT_MAX."); static_assert(enum_range::max > enum_range::min, "nameof::enum_range requires max > min."); @@ -299,27 +299,19 @@ template // Obtains simple (unqualified) string enum name of static storage enum variable. // This version is much lighter on the compile times and is not restricted to the enum_range limitation. template -[[nodiscard]] constexpr detail::enable_if_enum_t nameof_enum() noexcept { - using D = detail::remove_cvref_t; - static_assert(detail::nameof_enum_supported::value, "nameof::nameof_enum: Unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility)."); - static_assert(std::is_enum_v, "nameof::nameof_enum requires enum type."); - - return detail::nameof_enum_v; +[[nodiscard]] constexpr auto nameof_enum() noexcept -> detail::enable_if_enum_t { + return detail::nameof_enum_v, V>; } // 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)."); - return detail::nameof_type_v>; } // 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)."); - return detail::nameof_type_v; } @@ -335,7 +327,7 @@ template #define NAMEOF_RAW(...) ::nameof::detail::nameof_raw<::std::void_t>(#__VA_ARGS__) // Obtains simple (unqualified) string enum name of enum variable. -#define NAMEOF_ENUM(...) ::nameof::nameof_enum(__VA_ARGS__) +#define NAMEOF_ENUM(...) ::nameof::nameof_enum(__VA_ARGS__) // Obtains simple (unqualified) string enum name of static storage enum variable. // This version is much lighter on the compile times and is not restricted to the enum_range limitation.