[libcxx-commits] [libcxx] [libc++] Simplify the implementation of reserve() and shrink_to_fit() (PR #113453)

Benjamin Kramer via libcxx-commits libcxx-commits at lists.llvm.org
Fri Nov 29 04:20:14 PST 2024


================
@@ -3358,7 +3371,16 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void basic_string<_CharT, _Traits, _Allocator>::re
   if (__requested_capacity <= capacity())
     return;
 
-  __shrink_or_extend(__recommend(__requested_capacity));
+  __annotation_guard __g(*this);
+  auto __allocation = std::__allocate_at_least(__alloc_, __recommend(__requested_capacity) + 1);
+  auto __size       = size();
+  __begin_lifetime(__allocation.ptr, __allocation.count);
+  traits_type::copy(std::__to_address(__allocation.ptr), data(), __size + 1);
+  if (__is_long())
+    __alloc_traits::deallocate(__alloc_, __get_long_pointer(), __get_long_size() + 1);
----------------
d0k wrote:

This should pass capacity, not size.

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


More information about the libcxx-commits mailing list