From c947afdbae1061e07e0b16afa43534fc0cd7141c Mon Sep 17 00:00:00 2001 From: neargye Date: Wed, 2 Oct 2019 15:28:53 +0500 Subject: [PATCH] change check support compiler --- example/example.cpp | 6 +++--- include/magic_enum.hpp | 42 ++++++++++++++++++++++-------------------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/example/example.cpp b/example/example.cpp index ee00b1e..3f5a72e 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -33,7 +33,7 @@ int main() { std::cout << c1_name << std::endl; // RED // String enum name sequence. - constexpr auto& color_names = magic_enum::enum_names(); + constexpr auto color_names = magic_enum::enum_names(); std::cout << "Color names:"; for (auto n : color_names) { std::cout << " " << n; @@ -70,7 +70,7 @@ int main() { std::cout << "Color[0] = " << magic_enum::enum_value(0) << std::endl; // Color[0] = RED // Enum value sequence. - constexpr auto& colors = magic_enum::enum_values(); + constexpr auto colors = magic_enum::enum_values(); std::cout << "Colors sequence:"; for (Color c : colors) { std::cout << " " << c; // ostream operator for enum. @@ -106,7 +106,7 @@ int main() { // Enum pair (value enum, string enum name) sequence. constexpr auto color_entries = magic_enum::enum_entries(); std::cout << "Colors entries:"; - for (auto& e : color_entries) { + for (auto e : color_entries) { std::cout << " " << e.second << " = " << static_cast(e.first); } std::cout << std::endl; diff --git a/include/magic_enum.hpp b/include/magic_enum.hpp index 481498d..5907f98 100644 --- a/include/magic_enum.hpp +++ b/include/magic_enum.hpp @@ -56,6 +56,10 @@ namespace magic_enum { +#if defined(__clang__) || defined(__GNUC__) && __GNUC__>= 9 || defined(_MSC_VER) +# define MAGIC_ENUM_SUPPORTED 1 +#endif + // Enum value must be in range [MAGIC_ENUM_RANGE_MIN, MAGIC_ENUM_RANGE_MAX]. By default MAGIC_ENUM_RANGE_MIN = -128, MAGIC_ENUM_RANGE_MAX = 128. // If need another range for all enum types by default, redefine the macro MAGIC_ENUM_RANGE_MIN and MAGIC_ENUM_RANGE_MAX. // If need another range for specific enum type, add specialization enum_range for necessary enum type. @@ -79,7 +83,7 @@ namespace detail { template struct supported final -#if defined(__clang__) || defined(__GNUC__) && __GNUC__>= 9 || defined(_MSC_VER) || defined(MAGIC_ENUM_NO_CHECK_SUPPORT) +#if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED : std::true_type {}; #else : std::false_type {}; @@ -139,20 +143,19 @@ constexpr std::string_view pretty_name(std::string_view name) noexcept { template constexpr auto n() noexcept { static_assert(is_enum_v, "magic_enum::detail::n requires enum type."); -#if defined(__clang__) +#if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED +# if defined(__clang__) constexpr std::string_view name{__PRETTY_FUNCTION__ + 34, sizeof(__PRETTY_FUNCTION__) - 36}; -#elif defined(__GNUC__) +# elif defined(__GNUC__) constexpr std::string_view name{__PRETTY_FUNCTION__ + 49, sizeof(__PRETTY_FUNCTION__) - 51}; -#elif defined(_MSC_VER) +# elif defined(_MSC_VER) constexpr std::string_view name{__FUNCSIG__ + 40, sizeof(__FUNCSIG__) - 57}; +# endif + return static_string{name}; +#else + static_assert(supported::value, "magic_enum: Unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); + return std::string_view{}; // Unsupported compiler. #endif - - if constexpr (supported::value) { - return static_string{name}; - } else { - static_assert(supported::value, "magic_enum: Unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); - return std::string_view{}; // Unsupported compiler. - } } template @@ -161,18 +164,17 @@ inline constexpr auto type_name_v = n(); template constexpr auto n() noexcept { static_assert(is_enum_v, "magic_enum::detail::n requires enum type."); -#if defined(__clang__) || defined(__GNUC__) +#if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED +# if defined(__clang__) || defined(__GNUC__) constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2}); -#elif defined(_MSC_VER) +# elif defined(_MSC_VER) constexpr auto name = pretty_name({__FUNCSIG__, sizeof(__FUNCSIG__) - 17}); +# endif + return static_string{name}; +#else + static_assert(supported::value, "magic_enum: Unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); + return std::string_view{}; // Unsupported compiler. #endif - - if constexpr (supported::value) { - return static_string{name}; - } else { - static_assert(supported::value, "magic_enum: Unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); - return std::string_view{}; // Unsupported compiler. - } } template