From 706bec2af0a4872b962510277c28046abc75c827 Mon Sep 17 00:00:00 2001 From: qicosmos Date: Wed, 15 Jan 2025 11:54:06 +0800 Subject: [PATCH] type_string fit for msvc --- include/ylt/reflection/template_string.hpp | 16 +++++++++++++--- src/reflection/tests/test_reflection.cpp | 6 +++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/include/ylt/reflection/template_string.hpp b/include/ylt/reflection/template_string.hpp index 3dc33b50f..5a04cda83 100644 --- a/include/ylt/reflection/template_string.hpp +++ b/include/ylt/reflection/template_string.hpp @@ -32,10 +32,20 @@ template inline constexpr std::string_view type_string() { constexpr std::string_view sample = get_raw_name(); constexpr size_t prefix_length = sample.find("int"); - constexpr size_t suffix_length = sample.size() - prefix_length - 3; - constexpr std::string_view str = get_raw_name(); - return str.substr(prefix_length, str.size() - prefix_length - suffix_length); + constexpr size_t suffix_length = sample.size() - prefix_length - 3; + constexpr auto name = + str.substr(prefix_length, str.size() - prefix_length - suffix_length); +#if defined(_MSC_VER) + constexpr size_t space_pos = name.find(" "); + if constexpr (space_pos != std::string_view::npos) { + constexpr auto prefix = name.substr(0, space_pos); + if constexpr (prefix != "const" && prefix != "volatile") { + return name.substr(space_pos + 1); + } + } +#endif + return name; } template diff --git a/src/reflection/tests/test_reflection.cpp b/src/reflection/tests/test_reflection.cpp index 5a0e7ba28..02aa27878 100644 --- a/src/reflection/tests/test_reflection.cpp +++ b/src/reflection/tests/test_reflection.cpp @@ -495,15 +495,15 @@ TEST_CASE("test type_string") { #if defined(_MSC_VER) && !defined(__clang__) CHECK(type_string() == - "struct test_type_string::struct_test"); + "test_type_string::struct_test"); CHECK(type_string() == "const struct test_type_string::struct_test"); CHECK(type_string() == - "class test_type_string::class_test"); + "test_type_string::class_test"); CHECK(type_string() == "const class test_type_string::class_test"); CHECK(type_string() == - "union test_type_string::union_test"); + "test_type_string::union_test"); CHECK(type_string() == "const union test_type_string::union_test"); #else