diff --git a/include/nameof.hpp b/include/nameof.hpp index f54d219..12a256a 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -33,14 +33,22 @@ #include #include +#include #include #if !defined(NAMEOF_ENUM_MAX_SEARCH_DEPTH) -# define NAMEOF_ENUM_MAX_SEARCH_DEPTH 256 +# define NAMEOF_ENUM_MAX_SEARCH_DEPTH 128 #endif namespace nameof { +static_assert(NAMEOF_ENUM_MAX_SEARCH_DEPTH > 0, + "NAMEOF_ENUM_MAX_SEARCH_DEPTH must be positive and greater than zero."); +static_assert(NAMEOF_ENUM_MAX_SEARCH_DEPTH % 8 == 0, + "NAMEOF_ENUM_MAX_SEARCH_DEPTH must be a multiple of 8."); +static_assert(NAMEOF_ENUM_MAX_SEARCH_DEPTH < std::numeric_limits::max(), + "NAMEOF_ENUM_MAX_SEARCH_DEPTH must be less INT_MAX."); + namespace detail { template @@ -154,6 +162,10 @@ template template struct nameof_enum_t final { [[nodiscard]] constexpr std::string_view operator()(int value) const noexcept { + if constexpr (V > std::numeric_limits>::max()) { + return {"nameof_enum::out_of_range"}; + } + switch (value - V) { case 0: return nameof_enum_impl(V)>(); @@ -242,7 +254,8 @@ template >> template >>> [[nodiscard]] constexpr std::string_view nameof_enum(T value) noexcept { constexpr bool s = std::is_signed_v>>; - return detail::nameof_enum_t, s ? -NAMEOF_ENUM_MAX_SEARCH_DEPTH : 0>{}(static_cast(value)); + constexpr int min = s ? -MAGIC_ENUM_MAX_SEARCH_DEPTH : 0; + return detail::nameof_enum_t, min>{}(static_cast(value)); } template