Skip to content

Commit

Permalink
type_string fit for msvc
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Jan 15, 2025
1 parent 5cdf65a commit 706bec2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
16 changes: 13 additions & 3 deletions include/ylt/reflection/template_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,20 @@ template <typename T>
inline constexpr std::string_view type_string() {
constexpr std::string_view sample = get_raw_name<int>();
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<T>();
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 <auto T>
Expand Down
6 changes: 3 additions & 3 deletions src/reflection/tests/test_reflection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,15 +495,15 @@ TEST_CASE("test type_string") {

#if defined(_MSC_VER) && !defined(__clang__)
CHECK(type_string<test_type_string::struct_test>() ==
"struct test_type_string::struct_test");
"test_type_string::struct_test");
CHECK(type_string<const test_type_string::struct_test>() ==
"const struct test_type_string::struct_test");
CHECK(type_string<test_type_string::class_test>() ==
"class test_type_string::class_test");
"test_type_string::class_test");
CHECK(type_string<const test_type_string::class_test>() ==
"const class test_type_string::class_test");
CHECK(type_string<test_type_string::union_test>() ==
"union test_type_string::union_test");
"test_type_string::union_test");
CHECK(type_string<const test_type_string::union_test>() ==
"const union test_type_string::union_test");
#else
Expand Down

0 comments on commit 706bec2

Please sign in to comment.