[libcxx-commits] [libcxx] [libc++] Fix no-op shrink_to_fit for vector<bool> (PR #120495)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Mon Jan 20 08:21:20 PST 2025


================
@@ -20,19 +23,49 @@
 
 TEST_CONSTEXPR_CXX20 bool tests() {
   {
-    std::vector<bool> v(100);
+    using C = std::vector<bool>;
+    C v(100);
     v.push_back(1);
+    v.clear();
     v.shrink_to_fit();
-    assert(v.capacity() >= 101);
-    assert(v.size() >= 101);
+    assert(v.capacity() == 0);
----------------
ldionne wrote:

I don't think this assertion is portable, since `shrink_to_fit` is a non-binding request to shrink. So technically, I think the vector is free not to shrink even after being cleared. I would change this assertion to `assert(v.capacity() >= 0)` but that's not very useful, so perhaps `assert(v.capacity() <= before_cap)` is best for this test too.

However, I would also add

```
LIBCPP_ASSERT(v.capacity() == 0); // libc++ honors the shrink_to_fit request as a QOI matter
```

You could add a `LIBCPP_ASSERT` to the test below as well.

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


More information about the libcxx-commits mailing list