From e3dd165a4af89bd1b17efaa449557ce8d6ad283f Mon Sep 17 00:00:00 2001 From: Neargye Date: Sat, 25 Jan 2020 23:51:50 +0500 Subject: [PATCH] add compile check at enum_name --- include/magic_enum.hpp | 5 ++++- test/test.cpp | 4 ---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/include/magic_enum.hpp b/include/magic_enum.hpp index e7e833b..4a4c566 100644 --- a/include/magic_enum.hpp +++ b/include/magic_enum.hpp @@ -511,7 +511,10 @@ template // This version is much lighter on the compile times and is not restricted to the enum_range limitation. template [[nodiscard]] constexpr auto enum_name() noexcept -> detail::enable_if_enum_t { - return detail::name_v, V>; + constexpr std::string_view name = detail::name_v, V>; + static_assert(name.size() > 0, "Enum value does not have a name."); + + return name; } // Returns string enum name from enum value. diff --git a/test/test.cpp b/test/test.cpp index b5929ee..1f765f2 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -271,7 +271,6 @@ TEST_CASE("enum_name") { REQUIRE(cr_name == "RED"); REQUIRE(enum_name() == "BLUE"); REQUIRE(enum_name() == "GREEN"); - REQUIRE(enum_name(0)>().empty()); constexpr Numbers no = Numbers::one; constexpr auto no_name = enum_name(); @@ -279,7 +278,6 @@ TEST_CASE("enum_name") { REQUIRE(enum_name() == "two"); REQUIRE(enum_name() == "three"); REQUIRE(enum_name() == "many"); - REQUIRE(enum_name(0)>().empty()); constexpr Directions dr = Directions::Right; constexpr auto dr_name = enum_name(); @@ -287,7 +285,6 @@ TEST_CASE("enum_name") { REQUIRE(enum_name() == "Down"); REQUIRE(dr_name == "Right"); REQUIRE(enum_name() == "Left"); - REQUIRE(enum_name(0)>().empty()); constexpr number nt = number::three; constexpr auto nt_name = enum_name(); @@ -295,7 +292,6 @@ TEST_CASE("enum_name") { REQUIRE(enum_name() == "two"); REQUIRE(nt_name == "three"); REQUIRE(enum_name() == "four"); - REQUIRE(enum_name(0)>().empty()); } }