<div dir="ltr">Hi François,<div><br></div><div>I think you are absolutely correct. And this should definitely be addressed. I think the best would be for you to file a bug report on both libc++ and libstdc++.</div><div><br></div><div>Here are a few additional things I would point out:</div><div><br></div><div>Note, however, that this only affects vectors with elements of size 1 or less (like vector of bool), because any other type, of size 2 or above, can never have a pointer difference that would overflow ptrdiff_t. So, we are really only talking about char, unsigned char, bool, and empty classes.</div><div><br></div><div>I see only one sensible option to address this, which is to restrict max_size() to output the max positive value of ptrdiff_t. Something like this:</div><div><br></div><div> size_type max_size() const noexcept {</div><div> return ( PTRDIFF_MAX < /* alloc max-size */ ? PTRDIFF_MAX : /* alloc max-size */ );</div><div> }</div><div><br></div><div>It could also be conditional upon sizeof(T) such that this extra step is only done for elements of size 1.<br></div><div><br></div><div>The alternative of accounting for overflow on the calculation of size() would not only have undue overhead on size(), but it would also imply the same precaution be taken everywhere else (in iterators, in most mutating member functions, etc..). And I'm also not sure that this would be standard conforming since the difference_type would no longer be able to represent all iterator differences.</div><div><br></div><div>Cheers,</div><div>Mikael.</div><div><div class="gmail_extra">
</div></div></div>