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

Peng Liu via libcxx-commits libcxx-commits at lists.llvm.org
Mon Nov 25 12:37:02 PST 2024


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

>From e81abcc0d06d3e8dca8e039c01f8fcaead0fa019 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 e98154bc0807a7dd160cf73bd5d4b8499fab6a56 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      | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 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..f83b4af0c20557 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;
@@ -226,6 +229,16 @@ int main(int, char**) {
   }
   check_new_delete_called();
 
+  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();
+
 #if TEST_STD_VER >= 11
   try { // Throw in vector(initializer_list<value_type>) from type
     int throw_after = 1;



More information about the libcxx-commits mailing list