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

Peng Liu via libcxx-commits libcxx-commits at lists.llvm.org
Fri Nov 15 15:11:05 PST 2024


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

This PR simplifies the implementation of move constructor with alternative allocator by invoking `__init_with_size`, instead of calling `assign` which ultimately calls `__assign_with_size`. The advantage of using `__init_with_size` lies in its internal use of exception guard, thus simplifying the code. Furthermore, from a semantic standpoint, it is more intuitive for a constructor to call an initialization function rather than an assignment function.

>From 672e7cf95c64f885478ebf31b61079480ea86ea6 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] 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 d2d707d8c913c0..ac07750d8ece2f 100644
--- a/libcxx/include/__vector/vector.h
+++ b/libcxx/include/__vector/vector.h
@@ -976,9 +976,7 @@ vector<_Tp, _Allocator>::vector(vector&& __x, const __type_identity_t<allocator_
     __x.__begin_ = __x.__end_ = __x.__end_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());
   }
 }
 



More information about the libcxx-commits mailing list