From 86e7cfeec689d5219f08d3efb362b3a29ffc1968 Mon Sep 17 00:00:00 2001 From: saipubw Date: Mon, 19 Feb 2024 12:24:55 +0800 Subject: [PATCH] [struct_pack][bugfix] base class can't optimized as no container type in compile-time. (#599) --- include/ylt/struct_pack/type_calculate.hpp | 10 ++++++++-- src/struct_pack/tests/test_derived.cpp | 11 ++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/include/ylt/struct_pack/type_calculate.hpp b/include/ylt/struct_pack/type_calculate.hpp index 3991bd857..b53a0d22c 100644 --- a/include/ylt/struct_pack/type_calculate.hpp +++ b/include/ylt/struct_pack/type_calculate.hpp @@ -794,8 +794,14 @@ constexpr bool check_if_has_container() { return false; } else if constexpr (unique_ptr) { - return check_if_has_container< - remove_cvref_t, Arg, ParentArgs...>(); + if constexpr (is_base_class) { + // We can't make sure if derived class has container or not + return true; + } + else { + return check_if_has_container< + remove_cvref_t, Arg, ParentArgs...>(); + } } else if constexpr (id == type_id::container_t || id == type_id::string_t || diff --git a/src/struct_pack/tests/test_derived.cpp b/src/struct_pack/tests/test_derived.cpp index ce826e43d..742044313 100644 --- a/src/struct_pack/tests/test_derived.cpp +++ b/src/struct_pack/tests/test_derived.cpp @@ -141,7 +141,7 @@ TEST_CASE("test unique_ptr") { } } -TEST_CASE("test unique_ptr with virtual base") { +TEST_CASE("test vector> with virtual base") { using namespace test3; static_assert(struct_pack::detail::is_base_class); std::vector> vec; @@ -158,4 +158,13 @@ TEST_CASE("test unique_ptr with virtual base") { for (std::size_t i = 0; i < vec.size(); ++i) { CHECK(vec[i]->get_name() == vec2[i]->get_name()); } +} + +TEST_CASE("test unique_ptr with virtual base") { + using namespace test3; + std::unique_ptr ptr = std::make_unique(); + auto buffer2 = struct_pack::serialize(ptr); + auto res2 = struct_pack::deserialize>(buffer2); + CHECK(res2); + CHECK(res2.value()->get_name() == std::make_unique()->get_name()); } \ No newline at end of file