From 7f5e11edc66bd7b326bb1bace61fc96885ecd6cc Mon Sep 17 00:00:00 2001 From: Peng Liu Date: Fri, 15 Nov 2024 18:00:12 -0500 Subject: [PATCH 1/2] Refactor vector move-ctor with allocator --- libcxx/include/__vector/vector.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/libcxx/include/__vector/vector.h b/libcxx/include/__vector/vector.h index ae3ea1de61de0..4b9ba24e97f65 100644 --- a/libcxx/include/__vector/vector.h +++ b/libcxx/include/__vector/vector.h @@ -964,9 +964,7 @@ vector<_Tp, _Allocator>::vector(vector&& __x, const __type_identity_t _Ip; - auto __guard = std::__make_exception_guard(__destroy_vector(*this)); - assign(_Ip(__x.begin()), _Ip(__x.end())); - __guard.__complete(); + __init_with_size(_Ip(__x.begin()), _Ip(__x.end()), __x.size()); } } From e3566897d4993614a6f31dd3a504cd5e199783a1 Mon Sep 17 00:00:00 2001 From: Peng Liu Date: Mon, 25 Nov 2024 15:34:23 -0500 Subject: [PATCH 2/2] Add exception test for element-wise move ctor --- .../vector/vector.cons/exceptions.pass.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/libcxx/test/std/containers/sequences/vector/vector.cons/exceptions.pass.cpp b/libcxx/test/std/containers/sequences/vector/vector.cons/exceptions.pass.cpp index e2b0d691889c6..09355688042f9 100644 --- a/libcxx/test/std/containers/sequences/vector/vector.cons/exceptions.pass.cpp +++ b/libcxx/test/std/containers/sequences/vector/vector.cons/exceptions.pass.cpp @@ -17,6 +17,7 @@ #include #include "count_new.h" +#include "test_allocator.h" #include "test_iterators.h" template @@ -36,7 +37,9 @@ struct Allocator { void deallocate(T* ptr, std::size_t n) { std::allocator().deallocate(ptr, n); } template - friend bool operator==(const Allocator&, const Allocator&) { return true; } + friend bool operator==(const Allocator&, const Allocator&) { + return true; + } }; struct ThrowingT { @@ -138,7 +141,7 @@ int main(int, char**) { } catch (int) { } check_new_delete_called(); -#endif // TEST_STD_VER >= 14 +#endif // TEST_STD_VER >= 14 try { // Throw in vector(size_type, value_type, const allocator_type&) from the type int throw_after = 1; @@ -217,11 +220,12 @@ int main(int, char**) { } check_new_delete_called(); - try { // Throw in vector(vector&&, const allocator_type&) from type - std::vector > vec(Allocator(false)); - int throw_after = 1; - vec.emplace_back(throw_after); - std::vector > vec2(std::move(vec), Allocator(false)); + try { // Throw in vector(vector&&, const allocator_type&) from type during element-wise move + std::vector > vec(test_allocator(1)); + int throw_after = 10; + ThrowingT v(throw_after); + vec.insert(vec.end(), 6, v); + std::vector > vec2(std::move(vec), test_allocator(2)); } catch (int) { } check_new_delete_called();