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

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


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

### Overview of Changes
Added exception guard to the `vector(n, x, a)` 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 c88d6fce4f186771d18e6306a5fbb1f06b0fb1f9 Mon Sep 17 00:00:00 2001
From: winner245 <winner245 at hotmail.com>
Date: Sat, 19 Oct 2024 23:42:41 -0400
Subject: [PATCH] Add exception guard for vector(n, x, a) constructor

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

diff --git a/libcxx/include/vector b/libcxx/include/vector
index dc31f31838264c..700cb51f41eeb2 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