prevector: destroy elements only via erase()

Fixes a bug in which pop_back did not call the deleted item's destructor.

Using the most general erase() implementation to implement all the others
prevents similar bugs because the coupling between deallocation and destructor
invocation only needs to be maintained in one place.
Also reduces duplication of complex memmove logic.

(cherry picked from commit 1e2c29f2632c378843c5a3c9ad401a7bcacc4de0)
This commit is contained in:
Kaz Wesley 2016-04-13 10:09:16 -07:00 committed by Kris Nuttycombe
parent 89d9e557e1
commit d5844a45d5
1 changed files with 4 additions and 8 deletions

View File

@ -304,9 +304,8 @@ public:
} }
void resize(size_type new_size) { void resize(size_type new_size) {
while (size() > new_size) { if (size() > new_size) {
item_ptr(size() - 1)->~T(); erase(item_ptr(new_size), end());
_size--;
} }
if (new_size > capacity()) { if (new_size > capacity()) {
change_capacity(new_size); change_capacity(new_size);
@ -374,10 +373,7 @@ public:
} }
iterator erase(iterator pos) { iterator erase(iterator pos) {
(*pos).~T(); return erase(pos, pos + 1);
memmove(&(*pos), &(*pos) + 1, ((char*)&(*end())) - ((char*)(1 + &(*pos))));
_size--;
return pos;
} }
iterator erase(iterator first, iterator last) { iterator erase(iterator first, iterator last) {
@ -402,7 +398,7 @@ public:
} }
void pop_back() { void pop_back() {
_size--; erase(end() - 1, end());
} }
T& front() { T& front() {