[libcxx-commits] [libcxx] [libc++] Slightly simplify max_size and add new tests for vector (PR #119990)

Peng Liu via libcxx-commits libcxx-commits at lists.llvm.org
Thu Jan 30 06:04:14 PST 2025


================
@@ -0,0 +1,106 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <vector>
+
+// size_type max_size() const;
+
+#include <algorithm>
+#include <cassert>
+#include <climits>
+#include <cstdint>
+#include <limits>
+#include <memory>
+#include <type_traits>
+#include <vector>
+
+#include "min_allocator.h"
+#include "sized_allocator.h"
+#include "test_allocator.h"
+#include "test_macros.h"
+
+#if TEST_STD_VER >= 11
+
+template <typename Alloc>
+TEST_CONSTEXPR_CXX20 void test(const std::vector<bool, Alloc>& v) {
+  using Vector              = std::vector<bool, Alloc>;
+  using size_type           = typename Vector::size_type;
+  using difference_type     = typename Vector::difference_type;
+  using storage_type        = typename Vector::__storage_type;
----------------
winner245 wrote:

I've made part of the test libc++ only because libstdc++'s implementation is slightly different from libc++. The return values in the two implementations are, in math form (assuming no overflow)

```cpp
min(numeric_limits<difference_type>::max() - bits_per_word + 1, alloc_traits::max_size() * bits_per_word); // libstdc++
min(numeric_limits<difference_type>::max(), alloc_traits::max_size() * bits_per_word); // libc++, MSVC-STL
``` 

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


More information about the libcxx-commits mailing list