[libcxx-commits] [libcxx] cf7929f - [libc++][C++03] Cherry-pick #120577 and #120495 (#163748)

via libcxx-commits libcxx-commits at lists.llvm.org
Fri Oct 17 01:27:11 PDT 2025


Author: Nikolas Klauser
Date: 2025-10-17T10:27:07+02:00
New Revision: cf7929fb362cf6a79dce5d4649c97df13a526fc9

URL: https://github.com/llvm/llvm-project/commit/cf7929fb362cf6a79dce5d4649c97df13a526fc9
DIFF: https://github.com/llvm/llvm-project/commit/cf7929fb362cf6a79dce5d4649c97df13a526fc9.diff

LOG: [libc++][C++03] Cherry-pick #120577 and #120495 (#163748)

Added: 
    

Modified: 
    libcxx/include/__cxx03/vector
    libcxx/test/std/containers/sequences/vector.bool/shrink_to_fit.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__cxx03/vector b/libcxx/include/__cxx03/vector
index dbaa33c442948..43e82cd24b211 100644
--- a/libcxx/include/__cxx03/vector
+++ b/libcxx/include/__cxx03/vector
@@ -1630,7 +1630,7 @@ private:
     return __n * __bits_per_word;
   }
   _LIBCPP_HIDE_FROM_ABI static size_type __external_cap_to_internal(size_type __n) _NOEXCEPT {
-    return (__n - 1) / __bits_per_word + 1;
+    return __n > 0 ? (__n - 1) / __bits_per_word + 1 : size_type(0);
   }
 
 public:
@@ -2142,11 +2142,13 @@ void vector<bool, _Allocator>::reserve(size_type __n) {
 
 template <class _Allocator>
 void vector<bool, _Allocator>::shrink_to_fit() _NOEXCEPT {
-  if (__external_cap_to_internal(size()) > __cap()) {
+  if (__external_cap_to_internal(size()) < __cap()) {
 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
     try {
 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
-      vector(*this, allocator_type(__alloc())).swap(*this);
+      vector __v(*this, allocator_type(__alloc()));
+      if (__v.__cap() < __cap())
+        __v.swap(*this);
 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
     } catch (...) {
     }

diff  --git a/libcxx/test/std/containers/sequences/vector.bool/shrink_to_fit.pass.cpp b/libcxx/test/std/containers/sequences/vector.bool/shrink_to_fit.pass.cpp
index 665867a7bad4a..bf17733fa2739 100644
--- a/libcxx/test/std/containers/sequences/vector.bool/shrink_to_fit.pass.cpp
+++ b/libcxx/test/std/containers/sequences/vector.bool/shrink_to_fit.pass.cpp
@@ -11,8 +11,6 @@
 
 // void shrink_to_fit();
 
-// XFAIL: FROZEN-CXX03-HEADERS-FIXME
-
 #include <cassert>
 #include <climits>
 #include <vector>


        


More information about the libcxx-commits mailing list