[libcxx-commits] [libcxx] e6c2fdc - [libc++] Fix ambiguous call to std::max in vector<bool> (#119801)
via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Apr 2 08:14:18 PDT 2025
Author: Peng Liu
Date: 2025-04-02T11:14:14-04:00
New Revision: e6c2fdc90fe40edeb2e2ce85a24fc88b82644b29
URL: https://github.com/llvm/llvm-project/commit/e6c2fdc90fe40edeb2e2ce85a24fc88b82644b29
DIFF: https://github.com/llvm/llvm-project/commit/e6c2fdc90fe40edeb2e2ce85a24fc88b82644b29.diff
LOG: [libc++] Fix ambiguous call to std::max in vector<bool> (#119801)
Closes #121713.
Added:
libcxx/test/std/containers/sequences/vector.bool/small_allocator_size.pass.cpp
Modified:
libcxx/include/__vector/vector_bool.h
Removed:
################################################################################
diff --git a/libcxx/include/__vector/vector_bool.h b/libcxx/include/__vector/vector_bool.h
index 569cc5ea898bc..bc99470f126f6 100644
--- a/libcxx/include/__vector/vector_bool.h
+++ b/libcxx/include/__vector/vector_bool.h
@@ -549,7 +549,7 @@ vector<bool, _Allocator>::__recommend(size_type __new_size) const {
const size_type __cap = capacity();
if (__cap >= __ms / 2)
return __ms;
- return std::max(2 * __cap, __align_it(__new_size));
+ return std::max<size_type>(2 * __cap, __align_it(__new_size));
}
// Default constructs __n objects starting at __end_
diff --git a/libcxx/test/std/containers/sequences/vector.bool/small_allocator_size.pass.cpp b/libcxx/test/std/containers/sequences/vector.bool/small_allocator_size.pass.cpp
new file mode 100644
index 0000000000000..95e4c18cc7988
--- /dev/null
+++ b/libcxx/test/std/containers/sequences/vector.bool/small_allocator_size.pass.cpp
@@ -0,0 +1,102 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+// vector<bool>
+
+// XFAIL: FROZEN-CXX03-HEADERS-FIXME
+
+// This test ensures that std::vector<bool> handles allocator types with small size types
+// properly. Related issue: https://github.com/llvm/llvm-project/issues/121713.
+
+#include <cassert>
+#include <cstddef>
+#include <cstdint>
+#include <limits>
+#include <memory>
+#include <new>
+#include <vector>
+
+#include "sized_allocator.h"
+#include "test_macros.h"
+
+TEST_CONSTEXPR_CXX20 bool tests() {
+ {
+ using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
+ std::vector<bool, Alloc> c(Alloc(1));
+ c.resize(10);
+ assert(c.size() == 10);
+ assert(c.capacity() >= 10);
+ }
+ {
+ using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
+ std::vector<bool, Alloc> c(Alloc(1));
+ c.assign(10, true);
+ assert(c.size() == 10);
+ assert(c.capacity() >= 10);
+ }
+ {
+ using Alloc = sized_allocator<bool, std::uint8_t, std::int8_t>;
+ std::vector<bool, Alloc> c(Alloc(1));
+ c.insert(c.end(), true);
+ assert(c.size() == 1);
+ assert(c.capacity() >= 1);
+ }
+ {
+ using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
+ std::vector<bool, Alloc> c(Alloc(1));
+ c.insert(c.end(), 10, true);
+ assert(c.size() == 10);
+ assert(c.capacity() >= 10);
+ }
+ {
+ using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
+ std::vector<bool, Alloc> c(Alloc(1));
+ c.push_back(true);
+ assert(c.size() == 1);
+ assert(c.capacity() >= 1);
+ }
+ {
+ using Alloc = sized_allocator<bool, std::uint16_t, std::int16_t>;
+ std::vector<bool, Alloc> c(Alloc(1));
+ c.resize(10, true);
+ assert(c.size() == 10);
+ assert(c.capacity() >= 10);
+ }
+ {
+ using Alloc = sized_allocator<bool, std::uint32_t, std::int32_t>;
+ std::vector<bool, Alloc> c(Alloc(1));
+ c.resize(10);
+ assert(c.size() == 10);
+ assert(c.capacity() >= 10);
+ }
+ {
+ using Alloc = sized_allocator<bool, std::uint64_t, std::int64_t>;
+ std::vector<bool, Alloc> c(Alloc(1));
+ c.resize(10);
+ assert(c.size() == 10);
+ assert(c.capacity() >= 10);
+ }
+ {
+ using Alloc = sized_allocator<bool, std::size_t, std::ptr
diff _t>;
+ std::vector<bool, Alloc> c(Alloc(1));
+ c.resize(10);
+ assert(c.size() == 10);
+ assert(c.capacity() >= 10);
+ }
+
+ return true;
+}
+
+int main(int, char**) {
+ tests();
+#if TEST_STD_VER >= 20
+ static_assert(tests());
+#endif
+ return 0;
+}
More information about the libcxx-commits
mailing list