https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104191
Bug ID: 104191
Summary: Incorrect max_size() for node-based containers
Product: gcc
Version: 11.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: frankhb1989 at gmail dot com
Target Milestone: ---
Case:
#include <list>
#include <cassert>
template<class T>
struct one_alloc : std::allocator<T>
{
template<typename U> struct rebind { using other = one_alloc<U>;};
T* allocate(std::size_t n)
{
if(n > 1)
throw std::bad_array_new_length();
return std::allocator<T>::allocate(n);
}
std::size_t max_size() const noexcept
{
return 1;
}
};
int main()
{
std::list<int, one_alloc<int>> l;
l.push_back(0);
l.push_back(0);
assert(l.size() <= l.max_size());
}
This looks very wrong. The changes in
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=29134 seem too aggressive on
containers like list.
Logically, the container's max_size() should have nothing to do with the
allocator's max_size() (which limits the number of object of value_type in a
single allocation), and it should be solely determined by the internal node
count type. This is also consistent to the cases where the container's
size_type is always size_t (instead of size_type of the allocator).
There are some more subtleties concerned with
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78448. Not sure if extra checks
are required to make it conforming.