diff --git a/include/magic_enum.hpp b/include/magic_enum.hpp index d51792d..5897da2 100644 --- a/include/magic_enum.hpp +++ b/include/magic_enum.hpp @@ -876,34 +876,26 @@ template // Returns string name from enum-flags value. // If enum-flags value does not have name or value out of range, returns empty string. -template -[[nodiscard]] constexpr auto enum_name(E value) -> detail::enable_if_enum_flags_t> { +template +[[nodiscard]] auto enum_name(E value) -> detail::enable_if_enum_flags_t { using D = std::decay_t; using U = underlying_type_t; - if constexpr (Strict) { - for (std::size_t i = 0; i < detail::count_v; ++i) { - if (enum_value(i) == value) { - return detail::names_v[i]; - } - } - } else { - std::string name; - auto check_value = U{0}; - for (std::size_t i = 0; i < detail::count_v; ++i) { - if (const auto v = enum_value(i); (static_cast(value) & static_cast(v)) != 0) { - check_value |= static_cast(v); - const auto n = detail::names_v[i]; - if (!name.empty()) { - name.append(1, '|'); - } - name.append(n.data(), n.size()); + std::string name; + auto check_value = U{0}; + for (std::size_t i = 0; i < detail::count_v; ++i) { + if (const auto v = enum_value(i); (static_cast(value) & static_cast(v)) != 0) { + check_value |= static_cast(v); + const auto n = detail::names_v[i]; + if (!name.empty()) { + name.append(1, '|'); } + name.append(n.data(), n.size()); } + } - if (check_value == static_cast(value)) { - return name; - } + if (check_value == static_cast(value)) { + return name; } return {}; // Invalid value or out of range. diff --git a/test/test_flags.cpp b/test/test_flags.cpp index e6b7024..3c64000 100644 --- a/test/test_flags.cpp +++ b/test/test_flags.cpp @@ -556,48 +556,6 @@ TEST_CASE("enum_name") { REQUIRE(nto_name == "one|three"); REQUIRE(enum_name(static_cast(0)).empty()); } - - SECTION("strict automatic storage") { - constexpr Color cr = Color::RED; - constexpr auto cr_name = enum_name(cr); - Color cm[3] = {Color::RED, Color::GREEN, Color::BLUE}; - Color cb = Color::BLUE; - REQUIRE(cr_name == "RED"); - REQUIRE(enum_name(cb) == "BLUE"); - REQUIRE(enum_name(cm[1]) == "GREEN"); - REQUIRE(enum_name(Color::RED | Color{0}) == "RED"); - REQUIRE(enum_name(Color::RED | Color::GREEN).empty()); - REQUIRE(enum_name(Color::RED | Color{8}).empty()); - REQUIRE(enum_name(static_cast(0)).empty()); - - constexpr Numbers no = Numbers::one; - constexpr auto no_name = enum_name(no); - REQUIRE(no_name == "one"); - REQUIRE(enum_name(Numbers::two) == "two"); - REQUIRE(enum_name(Numbers::three) == "three"); - REQUIRE(enum_name(Numbers::many) == "many"); - REQUIRE(enum_name(Numbers::many | Numbers::two).empty()); - REQUIRE(enum_name(static_cast(0)).empty()); - - constexpr Directions dr = Directions::Right; - constexpr auto dr_name = enum_name(dr); - Directions du = Directions::Up; - REQUIRE(enum_name(du) == "Up"); - REQUIRE(enum_name(Directions::Down) == "Down"); - REQUIRE(dr_name == "Right"); - REQUIRE(enum_name(Directions::Left) == "Left"); - REQUIRE(enum_name(Directions::Right | Directions::Up | Directions::Left | Directions::Down).empty()); - REQUIRE(enum_name(static_cast(0)).empty()); - - constexpr number nto = number::three | number::one; - auto nto_name = enum_name(nto); - REQUIRE(enum_name(number::one) == "one"); - REQUIRE(enum_name(number::two) == "two"); - REQUIRE(enum_name(number::three) == "three"); - REQUIRE(enum_name(number::four) == "four"); - REQUIRE(nto_name.empty()); - REQUIRE(enum_name(static_cast(0)).empty()); - } } TEST_CASE("enum_names") {