Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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.
  • Loading branch information
kazcw committed Apr 16, 2016
commit 1e2c29f2632c378843c5a3c9ad401a7bcacc4de0
12 changes: 4 additions & 8 deletions src/prevector.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,8 @@ class prevector {
}

void resize(size_type new_size) {
while (size() > new_size) {
item_ptr(size() - 1)->~T();
_size--;
if (size() > new_size) {
erase(item_ptr(new_size), end());
}
if (new_size > capacity()) {
change_capacity(new_size);
Expand Down Expand Up @@ -368,10 +367,7 @@ class prevector {
}

iterator erase(iterator pos) {
(*pos).~T();
memmove(&(*pos), &(*pos) + 1, ((char*)&(*end())) - ((char*)(1 + &(*pos))));
_size--;
return pos;
return erase(pos, pos + 1);
}

iterator erase(iterator first, iterator last) {
Expand All @@ -396,7 +392,7 @@ class prevector {
}

void pop_back() {
_size--;
erase(end() - 1, end());
}

T& front() {
Expand Down