On Mon, Jul 10, 2006 at 03:58:19PM +1000, bringiton bringiton wrote:
This question is based on curiosity. How does std::vector::pop_back() call the destructor of the item getting removed?
i understood std::vector to be a contigious array of memory, therefore an item's memory does not go out of scope when being popped. ie the item goes out of scope when the entire array goes out of scope.
just look at the include files: bits/stl_vector.h says: void pop_back() { --this->_M_impl._M_finish; std::_Destroy(this->_M_impl._M_finish); } so the destructor is called in std::_Destroy. Which can be found in bits/stl_construct.h: /** * @if maint * Destroy the object pointed to by a pointer type. * @endif */ template<typename _Tp> inline void _Destroy(_Tp* __pointer) { __pointer->~_Tp(); } It just calls the destructor.