mirror of
https://github.com/Neargye/magic_enum.git
synced 2026-01-10 23:44:29 +00:00
add check to enum_to_string
This commit is contained in:
parent
100ca7ba8c
commit
d9ba68f01c
1 changed files with 3 additions and 0 deletions
|
|
@ -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));
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue