[libcxx-commits] [libcxx] Refactor vector constructors to eliminate code duplication (PR #113193)

via libcxx-commits libcxx-commits at lists.llvm.org
Mon Oct 21 10:17:18 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: None (winner245)

<details>
<summary>Changes</summary>

This PR refactors the `std::vector`'s initializer_list constructors `vector(initializer_list<value_type> __il, const allocator_type& __a)` and `vector(initializer_list<value_type> __il)` to reduce code duplication. The constructors now call `__init_with_size` directly, reducing code duplication and improving readability and maintainability.

---
Full diff: https://github.com/llvm/llvm-project/pull/113193.diff


1 Files Affected:

- (modified) libcxx/include/vector (+2-12) 


``````````diff
diff --git a/libcxx/include/vector b/libcxx/include/vector
index dc31f31838264c..5ee2ca52752e23 100644
--- a/libcxx/include/vector
+++ b/libcxx/include/vector
@@ -1292,24 +1292,14 @@ vector<_Tp, _Allocator>::vector(vector&& __x, const __type_identity_t<allocator_
 template <class _Tp, class _Allocator>
 _LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI
 vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il) {
-  auto __guard = std::__make_exception_guard(__destroy_vector(*this));
-  if (__il.size() > 0) {
-    __vallocate(__il.size());
-    __construct_at_end(__il.begin(), __il.end(), __il.size());
-  }
-  __guard.__complete();
+  __init_with_size(__il.begin(), __il.end(), __il.size());
 }
 
 template <class _Tp, class _Allocator>
 _LIBCPP_CONSTEXPR_SINCE_CXX20 inline _LIBCPP_HIDE_FROM_ABI
 vector<_Tp, _Allocator>::vector(initializer_list<value_type> __il, const allocator_type& __a)
     : __alloc_(__a) {
-  auto __guard = std::__make_exception_guard(__destroy_vector(*this));
-  if (__il.size() > 0) {
-    __vallocate(__il.size());
-    __construct_at_end(__il.begin(), __il.end(), __il.size());
-  }
-  __guard.__complete();
+  __init_with_size(__il.begin(), __il.end(), __il.size());;
 }
 
 #endif // _LIBCPP_CXX03_LANG

``````````

</details>


https://github.com/llvm/llvm-project/pull/113193


More information about the libcxx-commits mailing list