[libc++] Fix ambiguous call to std::max in vector<bool> (#119801)

Closes #121713.
This commit is contained in:
Peng Liu 2025-04-02 11:14:14 -04:00 committed by GitHub
parent ffaaaceaa1
commit e6c2fdc90f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 103 additions and 1 deletions

View File

@ -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_

View File

@ -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::ptrdiff_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;
}