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

via libcxx-commits libcxx-commits at lists.llvm.org
Sun Oct 20 07:08:11 PDT 2024


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

Added exception guard to the `vector(n, x, a)` constructor to enhance exception safety. This change ensures that the `vector(n, x, a)` constructor is consistent with other constructors, such as  `vector(n)`, `vector(n, x)`, `vector(n, a)`, in terms of exception safety.

>From 8b17923a391dee3e903af4f4388f3cfe2c930708 Mon Sep 17 00:00:00 2001
From: Peng Liu <winner245 at hotmail.com>
Date: Sun, 20 Oct 2024 10:04:24 -0400
Subject: [PATCH] Add exception guard for constructor vector(n, x, a)

---
 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