[libcxx-commits] [libcxx] [libc++] Fix shrink_to_fit to swap buffer only when capacity is strictly smaller (PR #127321)

Peng Liu via libcxx-commits libcxx-commits at lists.llvm.org
Tue Feb 18 05:54:23 PST 2025


================
@@ -49,4 +50,89 @@ TEST_CONSTEXPR_CXX20 bool operator==(increasing_allocator<T>, increasing_allocat
   return true;
 }
 
+template <std::size_t MinAllocSize, typename T>
+class min_size_allocator {
+public:
+  using value_type     = T;
+  min_size_allocator() = default;
+
+  template <typename U>
+  TEST_CONSTEXPR_CXX20 min_size_allocator(const min_size_allocator<MinAllocSize, U>&) TEST_NOEXCEPT {}
+
+#if TEST_STD_VER >= 23
+  TEST_CONSTEXPR_CXX23 std::allocation_result<T*> allocate_at_least(std::size_t n) {
+    if (n < MinAllocSize)
+      n = MinAllocSize;
+    return std::allocator<T>{}.allocate_at_least(n);
+  }
+#endif // TEST_STD_VER >= 23
----------------
winner245 wrote:

That's what I mean. Providing a preferred method, i.e., `allocate_at_least` is meaningful for C++23 and later. Providing `allocate` as a fallback is meaningful for C++20 and earlier. So why should I remove it?

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


More information about the libcxx-commits mailing list