[libcxx-commits] [libcxx] Add exception guard for vector(n, x, a) ctor (PR #113069)

via libcxx-commits libcxx-commits at lists.llvm.org
Sat Oct 19 20:21:43 PDT 2024


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

### Overview of Changes
Added an exception guard to the `vector` constructor to enhance exception safety.

### Details
This change ensures that the `vector(n, x, a)` constructor is consistent with other constructors, such as `vector(n, x)` and `vector(n, a)`, in terms of exception safety.

### Impact
Improves robustness and resource management during exceptions.

>From bb81acdab3661f6ced081902a14243d5c18ab8b1 Mon Sep 17 00:00:00 2001
From: winner245 <winner245 at hotmail.com>
Date: Sat, 19 Oct 2024 23:16:40 -0400
Subject: [PATCH] Add exception guard for vector(n, x, a) ctor

---
 libcxx/include/vector | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libcxx/include/vector b/libcxx/include/vector
index dc31f31838264c..0aa4dee1bca8b3 100644
--- a/libcxx/include/vector
+++ b/libcxx/include/vector
@@ -487,10 +487,12 @@ public:
   _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
   vector(size_type __n, const value_type& __x, const allocator_type& __a)
       : __alloc_(__a) {
+		auto __guard = std::__make_exception_guard(__destroy_vector(*this));
     if (__n > 0) {
       __vallocate(__n);
       __construct_at_end(__n, __x);
     }
+		__guard.__complete();
   }
 
   template <class _InputIterator,



More information about the libcxx-commits mailing list