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

add check to enum_to_string

This commit is contained in:
terik23 2019-03-31 20:29:02 +05:00
parent 100ca7ba8c
commit d9ba68f01c

View file

@ -170,6 +170,9 @@ template <typename T, typename = std::enable_if_t<std::is_enum_v<std::decay_t<T>
[[nodiscard]] constexpr std::optional<std::string_view> enum_to_string(T value) noexcept {
constexpr bool s = std::is_signed_v<std::underlying_type_t<std::decay_t<T>>>;
constexpr int min = s ? -MAGIC_ENUM_MAX_SEARCH_DEPTH : 0;
if (static_cast<int>(value) >= MAGIC_ENUM_MAX_SEARCH_DEPTH || static_cast<int>(value) <= -MAGIC_ENUM_MAX_SEARCH_DEPTH) {
return std::nullopt; // Enum variable out of range MAGIC_ENUM_MAX_SEARCH_DEPTH.
}
return detail::enum_to_string_impl_t<std::decay_t<T>, min>{}(static_cast<int>(value));
}