Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libcxx/include/__vector/vector_bool.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}