https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109442
--- Comment #27 from Jonathan Wakely <redi at gcc dot gnu.org> --- vector::size() is called **very often** so needs to be as fast as possible. Does this still inline identically? I tried something like that in r14-1452-gfb409a15d9babc and reverted it in r14-1470-gb7b255e77a2719, but my _M_invariant() function was more complicated. Maybe the other part of my _M_invariant() function could be added directly to capacity() --- a/libstdc++-v3/include/bits/stl_vector.h +++ b/libstdc++-v3/include/bits/stl_vector.h @@ -1201,8 +1201,11 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER size_type capacity() const _GLIBCXX_NOEXCEPT { - return size_type(this->_M_impl._M_end_of_storage - - this->_M_impl._M_start); + ptrdiff_t __dif + = this->_M_impl._M_end_of_storage - this->_M_impl._M_start; + if (__dir < 0) + __builtin_unreachable(); + return size_type(__dif); } /**