Skip to content

Commit 1e2c29f

Browse files
committed
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.
1 parent 73fc922 commit 1e2c29f

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

src/prevector.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,8 @@ class prevector {
298298
}
299299

300300
void resize(size_type new_size) {
301-
while (size() > new_size) {
302-
item_ptr(size() - 1)->~T();
303-
_size--;
301+
if (size() > new_size) {
302+
erase(item_ptr(new_size), end());
304303
}
305304
if (new_size > capacity()) {
306305
change_capacity(new_size);
@@ -368,10 +367,7 @@ class prevector {
368367
}
369368

370369
iterator erase(iterator pos) {
371-
(*pos).~T();
372-
memmove(&(*pos), &(*pos) + 1, ((char*)&(*end())) - ((char*)(1 + &(*pos))));
373-
_size--;
374-
return pos;
370+
return erase(pos, pos + 1);
375371
}
376372

377373
iterator erase(iterator first, iterator last) {
@@ -396,7 +392,7 @@ class prevector {
396392
}
397393

398394
void pop_back() {
399-
_size--;
395+
erase(end() - 1, end());
400396
}
401397

402398
T& front() {

0 commit comments

Comments
 (0)