diff --git a/include/stdx/ct_conversions.hpp b/include/stdx/ct_conversions.hpp index 871d457..64ca586 100644 --- a/include/stdx/ct_conversions.hpp +++ b/include/stdx/ct_conversions.hpp @@ -36,23 +36,39 @@ template CONSTEVAL static auto enum_as_string() -> std::basic_string_view { #ifdef __clang__ constexpr std::string_view value_string = __PRETTY_FUNCTION__; - constexpr auto rhs = value_string.size() - 2; #elif defined(__GNUC__) || defined(__GNUG__) constexpr std::string_view value_string = __PRETTY_FUNCTION__; - constexpr auto rhs = value_string.size() - 2; #else static_assert(always_false_v, "Unknown compiler, can't build type name."); #endif + constexpr auto qual_str = [&]() -> std::string_view { + constexpr auto rhs = value_string.size() - 2; + if (auto const eq_pos = value_string.find_last_of('='); + eq_pos != std::string_view::npos) { + auto const lhs = eq_pos + 2; + return value_string.substr(lhs, rhs - lhs + 1); + } + return value_string; + }(); + + constexpr auto cast_str = [&]() -> std::string_view { + if (auto const close_paren_pos = qual_str.find_last_of(')'); + close_paren_pos != std::string_view::npos) { + if (qual_str[close_paren_pos + 1] != ':') { + return qual_str; + } + } - constexpr auto lhs = [&]() -> std::string_view::size_type { - if (auto const colon_pos = value_string.find_last_of(':'); + if (auto const colon_pos = qual_str.find_last_of(':'); colon_pos != std::string_view::npos) { - return colon_pos + 1; + return qual_str.substr(colon_pos + 1); } - return 0; + + return qual_str; }(); - return value_string.substr(lhs, rhs - lhs + 1); + + return cast_str; } } // namespace v1 } // namespace stdx diff --git a/test/ct_conversions.cpp b/test/ct_conversions.cpp index f76ab09..e9fd1c6 100644 --- a/test/ct_conversions.cpp +++ b/test/ct_conversions.cpp @@ -34,6 +34,13 @@ enum struct B { Y }; TEST_CASE("enum as string", "[ct_conversion]") { using namespace std::string_view_literals; - STATIC_REQUIRE(stdx::enum_as_string() == "X"sv); - STATIC_REQUIRE(stdx::enum_as_string() == "Y"sv); + STATIC_CHECK(stdx::enum_as_string() == "X"sv); + STATIC_CHECK(stdx::enum_as_string() == "Y"sv); +} + +enum struct C {}; + +TEST_CASE("enum as string (no identifier)", "[ct_conversion]") { + using namespace std::string_view_literals; + STATIC_CHECK(stdx::enum_as_string() == "(C)17"sv); }