I was looking through the std::vector code and I found something I didn't quite get. When capacity < size() + 1 it needs to reallocate the buffer so it can insert the new element. What it does (as far as I've been able to extract from the code) is:
- allocate the new buffer
- copy the prefix of the old buffer (0 - index of insertion)
- construct the new element in the new buffer
- copy the suffix of the old buffer (index - end)
- call destructor on all items in old buffer
- deallocate old buffer
The prefix and suffix copy is done with memmove as far as I could see. Isn't memmove a pure binary copy of the data? It doesn't call the constructor of the elements, does it? What I was wondering is, why does the function call the destructor on the elements in the old buffer if the memory is just moved, not re-constructed in the new buffer?