https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90192
--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> --- Something like this: --- a/libstdc++-v3/include/bits/stl_vector.h +++ b/libstdc++-v3/include/bits/stl_vector.h @@ -1153,8 +1153,48 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER void resize(size_type __new_size, const value_type& __x) { - if (__new_size > size()) - _M_fill_insert(end(), __new_size - size(), __x); + if (__new_size > capacity()) + { + struct _Guard : _Guard_alloc + { + // Elements to destroy: + pointer _M_first{}; + pointer _M_last{}; + + using _Guard_alloc::_Guard_alloc; + + _GLIBCXX20_CONSTEXPR + ~_Guard() + { + std::_Destroy(this->_M_first, this->_M_last, + this->_M_vect._M_get_Tp_allocator()); + } + }; + + allocator_type& __a = _M_get_Tp_allocator(); + const size_type __sz = size(); + const size_type __n = __new_size - size(); + const pointer __start = this->_M_allocate(__new_size); + _Guard __guard(__start, __new_size, *this); + const pointer __mid = __start + __sz; + __guard._M_last + = std::__uninitialized_fill_n_a(__mid, __n, __x, __a); + __guard._M_first = __mid; + std::__uninitialized_move_if_noexcept_a(this->_M_impl._M_start, + this->_M_impl._M_finish, + __start, __a); + __guard._M_storage = this->_M_impl._M_start; + __guard._M_first = this->_M_impl._M_start; + __guard._M_last = this->_M_impl._M_last; + this->_M_impl._M_start = __start; + this->_M_impl._M_finish = __start + __new_size; + this->_M_impl._M_end_of_storage = this->_M_impl._M_finish; + } + else if (__new_size > size()) + this->_M_impl._M_finish + = std::__uninitialized_fill_n_a(this->_M_impl._M_finish, + __new_size - size(), + __x, _M_get_Tp_allocator()); else if (__new_size < size()) _M_erase_at_end(this->_M_impl._M_start + __new_size); }