https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110287
--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> --- (In reply to Jan Hubicka from comment #5) > > Do you mean something like this? > I sent my own version, but yours looks nicer. > > > > diff --git a/libstdc++-v3/include/bits/stl_vector.h > > b/libstdc++-v3/include/bits/stl_vector.h > > index 70ced3d101f..a4dbfeb8b5b 100644 > > --- a/libstdc++-v3/include/bits/stl_vector.h > > +++ b/libstdc++-v3/include/bits/stl_vector.h > > @@ -1902,6 +1902,21 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER > > return (__len < size() || __len > max_size()) ? max_size() : __len; > > } > > > > + // Called by _M_insert_aux etc. > > + _GLIBCXX20_CONSTEXPR > > + size_type > > + _M_check_len_1(const char* __s) const > > + { > > + if (__builtin_constant_p(size())) > Perhaps ruling this out for 32bit ports? Or can we assume that half of > address space can never be allocated in single block? No, I don't think we can assume that. > > + { > > + if (size() == 0) > > + return 1; > > + else if (size() < max_size() / 2) > I think even this conditional is safe to be assumed to be true, > since we can not allocate half of 64bit address space. There is no guarantee that std::vector<T, A>::max_size() is PTRDIFF_MAX. It depends on the Allocator type, A. A user-defined allocator could have max_size() == 100. > In general it is importnat to not fall through to _M_check_len. > > As I noticed, we may want to use assume attribute to make clear that > retval <= max_size () > to avoid other unnecesary throws downstream. > > Honza