1
0
Fork 0
mirror of https://github.com/Neargye/magic_enum.git synced 2026-01-10 23:44:29 +00:00

enum_name returns string_view

This commit is contained in:
terik23 2019-04-21 20:52:14 +05:00
parent 8ddfae18d1
commit 9dc22008a7
4 changed files with 25 additions and 36 deletions

View file

@ -295,15 +295,10 @@ template <typename E, typename = detail::enable_if_enum_t<E>>
// Obtains string enum name from enum value.
template <typename E, typename D = std::decay_t<E>, typename = detail::enable_if_enum_t<D>>
[[nodiscard]] constexpr std::optional<std::string_view> enum_name(E value) noexcept {
[[nodiscard]] constexpr std::string_view enum_name(E value) noexcept {
static_assert(std::is_enum_v<D>, "magic_enum::enum_name requires enum type.");
const auto name = detail::name_impl<D>(static_cast<int>(value));
if (name.empty()) {
return std::nullopt; // Invalid value or out of range.
} else {
return name;
}
return detail::name_impl<D>(static_cast<int>(value));
}
// Obtains string enum name sequence.