diff --git a/include/nameof.hpp b/include/nameof.hpp index ec31083..0804d20 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -578,7 +578,7 @@ constexpr auto n() noexcept { # elif defined(_MSC_VER) constexpr std::string_view name{__FUNCSIG__ + 63, sizeof(__FUNCSIG__) - 81 - (__FUNCSIG__[sizeof(__FUNCSIG__) - 19] == ' ' ? 1 : 0)}; # endif - static_assert(!name.empty(), "Type does not have a name."); + static_assert(name.size() > 0, "Type does not have a name."); return cstring{name}; #else @@ -604,8 +604,13 @@ template // Obtains simple (unqualified) string enum name of static storage enum variable. // This version is much lighter on the compile times and is not restricted to the enum_range limitation. template -[[nodiscard]] constexpr auto nameof_enum() noexcept -> detail::enable_if_enum_t { - return detail::enum_name_v, V>; +[[nodiscard]] constexpr auto nameof_enum() noexcept { + using D = detail::remove_cvref_t; + static_assert(std::is_enum_v, "nameof::nameof_enum requires enum type."); + constexpr auto name = detail::n(); + static_assert(name.size() > 0, "Enum value does not have a name."); + + return cstring{name}; } // Obtains string name of type, reference and cv-qualifiers are ignored. @@ -634,7 +639,7 @@ template #define NAMEOF(...) []() constexpr noexcept { \ ::std::void_t(); \ constexpr auto name = ::nameof::detail::pretty_name(#__VA_ARGS__, true); \ - static_assert(!name.empty(), "Expression does not have a name."); \ + static_assert(name.size() > 0, "Expression does not have a name."); \ constexpr auto size = name.size(); \ return ::nameof::cstring{name}; }() @@ -642,16 +647,16 @@ template #define NAMEOF_FULL(...) []() constexpr noexcept { \ ::std::void_t(); \ constexpr auto name = ::nameof::detail::pretty_name(#__VA_ARGS__, false); \ - static_assert(!name.empty(), "Expression does not have a name."); \ + static_assert(name.size() > 0, "Expression does not have a name."); \ constexpr auto size = name.size(); \ return ::nameof::cstring{name}; }() // Obtains raw string name of variable, function, macro. -#define NAMEOF_RAW(...) []() constexpr noexcept { \ - ::std::void_t(); \ - constexpr auto name = ::std::string_view{#__VA_ARGS__}; \ - static_assert(!name.empty(), "Expression does not have a name."); \ - constexpr auto size = name.size(); \ +#define NAMEOF_RAW(...) []() constexpr noexcept { \ + ::std::void_t(); \ + constexpr auto name = ::std::string_view{#__VA_ARGS__}; \ + static_assert(name.size() > 0, "Expression does not have a name."); \ + constexpr auto size = name.size(); \ return ::nameof::cstring{name}; }() // Obtains simple (unqualified) string enum name of enum variable. diff --git a/test/test.cpp b/test/test.cpp index a115b16..dd57d1d 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -270,7 +270,6 @@ TEST_CASE("NAMEOF_CONST_ENUM") { REQUIRE(cr_name == "RED"); REQUIRE(NAMEOF_CONST_ENUM(Color::BLUE) == "BLUE"); REQUIRE(NAMEOF_CONST_ENUM(cm[1]) == "GREEN"); - REQUIRE(NAMEOF_CONST_ENUM(static_cast(0)).empty()); constexpr Numbers no = Numbers::one; constexpr auto no_name = NAMEOF_CONST_ENUM(no); @@ -278,7 +277,6 @@ TEST_CASE("NAMEOF_CONST_ENUM") { REQUIRE(NAMEOF_CONST_ENUM(Numbers::two) == "two"); REQUIRE(NAMEOF_CONST_ENUM(Numbers::three) == "three"); REQUIRE(NAMEOF_CONST_ENUM(Numbers::many) == "many"); - REQUIRE(NAMEOF_CONST_ENUM(static_cast(0)).empty()); constexpr Directions dr = Directions::Right; constexpr auto dr_name = NAMEOF_CONST_ENUM(dr); @@ -286,7 +284,6 @@ TEST_CASE("NAMEOF_CONST_ENUM") { REQUIRE(NAMEOF_CONST_ENUM(Directions::Down) == "Down"); REQUIRE(dr_name == "Right"); REQUIRE(NAMEOF_CONST_ENUM(Directions::Left) == "Left"); - REQUIRE(NAMEOF_CONST_ENUM(static_cast(0)).empty()); constexpr number nt = number::three; constexpr auto nt_name = NAMEOF_CONST_ENUM(nt); @@ -294,7 +291,6 @@ TEST_CASE("NAMEOF_CONST_ENUM") { REQUIRE(NAMEOF_CONST_ENUM(number::two) == "two"); REQUIRE(nt_name == "three"); REQUIRE(NAMEOF_CONST_ENUM(number::four) == "four"); - REQUIRE(NAMEOF_CONST_ENUM(static_cast(0)).empty()); } TEST_CASE("nameof_enum") { @@ -339,7 +335,6 @@ TEST_CASE("nameof_enum") { REQUIRE(cr_name == "RED"); REQUIRE(nameof::nameof_enum() == "BLUE"); REQUIRE(nameof::nameof_enum() == "GREEN"); - REQUIRE(nameof::nameof_enum(0)>().empty()); constexpr Numbers no = Numbers::one; constexpr auto no_name = nameof::nameof_enum(); @@ -347,7 +342,6 @@ TEST_CASE("nameof_enum") { REQUIRE(nameof::nameof_enum() == "two"); REQUIRE(nameof::nameof_enum() == "three"); REQUIRE(nameof::nameof_enum() == "many"); - REQUIRE(nameof::nameof_enum(0)>().empty()); constexpr Directions dr = Directions::Right; constexpr auto dr_name = nameof::nameof_enum(); @@ -355,7 +349,6 @@ TEST_CASE("nameof_enum") { REQUIRE(nameof::nameof_enum() == "Down"); REQUIRE(dr_name == "Right"); REQUIRE(nameof::nameof_enum() == "Left"); - REQUIRE(nameof::nameof_enum(0)>().empty()); constexpr number nt = number::three; constexpr auto nt_name = nameof::nameof_enum(); @@ -363,7 +356,6 @@ TEST_CASE("nameof_enum") { REQUIRE(nameof::nameof_enum() == "two"); REQUIRE(nt_name == "three"); REQUIRE(nameof::nameof_enum() == "four"); - REQUIRE(nameof::nameof_enum(0)>().empty()); } }