[libcxx-commits] [libcxx] [libc++] Refactor vector move constructor with allocator (PR #116449)

Peng Liu via libcxx-commits libcxx-commits at lists.llvm.org
Tue Nov 26 04:08:03 PST 2024


https://github.com/winner245 updated https://github.com/llvm/llvm-project/pull/116449

>From 7f5e11edc66bd7b326bb1bace61fc96885ecd6cc Mon Sep 17 00:00:00 2001
From: Peng Liu <winner245 at hotmail.com>
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 ae3ea1de61de01..4b9ba24e97f65e 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<allocator_
     __x.__begin_ = __x.__end_ = __x.__cap_ = nullptr;
   } else {
     typedef move_iterator<iterator> _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 <winner245 at hotmail.com>
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 e2b0d691889c6c..09355688042f9a 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 <vector>
 
 #include "count_new.h"
+#include "test_allocator.h"
 #include "test_iterators.h"
 
 template <class T>
@@ -36,7 +37,9 @@ struct Allocator {
   void deallocate(T* ptr, std::size_t n) { std::allocator<T>().deallocate(ptr, n); }
 
   template <class U>
-  friend bool operator==(const Allocator&, const Allocator<U>&) { return true; }
+  friend bool operator==(const Allocator&, const Allocator<U>&) {
+    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<ThrowingT, Allocator<ThrowingT> > vec(Allocator<ThrowingT>(false));
-    int throw_after = 1;
-    vec.emplace_back(throw_after);
-    std::vector<ThrowingT, Allocator<ThrowingT> > vec2(std::move(vec), Allocator<ThrowingT>(false));
+  try { // Throw in vector(vector&&, const allocator_type&) from type during element-wise move
+    std::vector<ThrowingT, test_allocator<ThrowingT> > vec(test_allocator<ThrowingT>(1));
+    int throw_after = 10;
+    ThrowingT v(throw_after);
+    vec.insert(vec.end(), 6, v);
+    std::vector<ThrowingT, test_allocator<ThrowingT> > vec2(std::move(vec), test_allocator<ThrowingT>(2));
   } catch (int) {
   }
   check_new_delete_called();



More information about the libcxx-commits mailing list