mirror of
https://github.com/Neargye/magic_enum.git
synced 2026-01-10 23:44:29 +00:00
add compile check at enum_name
This commit is contained in:
parent
6b220c83fc
commit
e3dd165a4a
2 changed files with 4 additions and 5 deletions
|
|
@ -511,7 +511,10 @@ template <typename E>
|
||||||
// This version is much lighter on the compile times and is not restricted to the enum_range limitation.
|
// This version is much lighter on the compile times and is not restricted to the enum_range limitation.
|
||||||
template <auto V>
|
template <auto V>
|
||||||
[[nodiscard]] constexpr auto enum_name() noexcept -> detail::enable_if_enum_t<decltype(V), std::string_view> {
|
[[nodiscard]] constexpr auto enum_name() noexcept -> detail::enable_if_enum_t<decltype(V), std::string_view> {
|
||||||
return detail::name_v<std::decay_t<decltype(V)>, V>;
|
constexpr std::string_view name = detail::name_v<std::decay_t<decltype(V)>, V>;
|
||||||
|
static_assert(name.size() > 0, "Enum value does not have a name.");
|
||||||
|
|
||||||
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns string enum name from enum value.
|
// Returns string enum name from enum value.
|
||||||
|
|
|
||||||
|
|
@ -271,7 +271,6 @@ TEST_CASE("enum_name") {
|
||||||
REQUIRE(cr_name == "RED");
|
REQUIRE(cr_name == "RED");
|
||||||
REQUIRE(enum_name<Color::BLUE>() == "BLUE");
|
REQUIRE(enum_name<Color::BLUE>() == "BLUE");
|
||||||
REQUIRE(enum_name<cm[1]>() == "GREEN");
|
REQUIRE(enum_name<cm[1]>() == "GREEN");
|
||||||
REQUIRE(enum_name<static_cast<Color>(0)>().empty());
|
|
||||||
|
|
||||||
constexpr Numbers no = Numbers::one;
|
constexpr Numbers no = Numbers::one;
|
||||||
constexpr auto no_name = enum_name<no>();
|
constexpr auto no_name = enum_name<no>();
|
||||||
|
|
@ -279,7 +278,6 @@ TEST_CASE("enum_name") {
|
||||||
REQUIRE(enum_name<Numbers::two>() == "two");
|
REQUIRE(enum_name<Numbers::two>() == "two");
|
||||||
REQUIRE(enum_name<Numbers::three>() == "three");
|
REQUIRE(enum_name<Numbers::three>() == "three");
|
||||||
REQUIRE(enum_name<Numbers::many>() == "many");
|
REQUIRE(enum_name<Numbers::many>() == "many");
|
||||||
REQUIRE(enum_name<static_cast<Numbers>(0)>().empty());
|
|
||||||
|
|
||||||
constexpr Directions dr = Directions::Right;
|
constexpr Directions dr = Directions::Right;
|
||||||
constexpr auto dr_name = enum_name<dr>();
|
constexpr auto dr_name = enum_name<dr>();
|
||||||
|
|
@ -287,7 +285,6 @@ TEST_CASE("enum_name") {
|
||||||
REQUIRE(enum_name<Directions::Down>() == "Down");
|
REQUIRE(enum_name<Directions::Down>() == "Down");
|
||||||
REQUIRE(dr_name == "Right");
|
REQUIRE(dr_name == "Right");
|
||||||
REQUIRE(enum_name<Directions::Left>() == "Left");
|
REQUIRE(enum_name<Directions::Left>() == "Left");
|
||||||
REQUIRE(enum_name<static_cast<Directions>(0)>().empty());
|
|
||||||
|
|
||||||
constexpr number nt = number::three;
|
constexpr number nt = number::three;
|
||||||
constexpr auto nt_name = enum_name<nt>();
|
constexpr auto nt_name = enum_name<nt>();
|
||||||
|
|
@ -295,7 +292,6 @@ TEST_CASE("enum_name") {
|
||||||
REQUIRE(enum_name<number::two>() == "two");
|
REQUIRE(enum_name<number::two>() == "two");
|
||||||
REQUIRE(nt_name == "three");
|
REQUIRE(nt_name == "three");
|
||||||
REQUIRE(enum_name<number::four>() == "four");
|
REQUIRE(enum_name<number::four>() == "four");
|
||||||
REQUIRE(enum_name<static_cast<number>(0)>().empty());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue