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.
This commit is contained in:
parent
73fc922ed6
commit
1e2c29f263
1 changed files with 4 additions and 8 deletions
|
@ -298,9 +298,8 @@ public:
|
|||
}
|
||||
|
||||
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);
|
||||
|
@ -368,10 +367,7 @@ public:
|
|||
}
|
||||
|
||||
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) {
|
||||
|
@ -396,7 +392,7 @@ public:
|
|||
}
|
||||
|
||||
void pop_back() {
|
||||
_size--;
|
||||
erase(end() - 1, end());
|
||||
}
|
||||
|
||||
T& front() {
|
||||
|
|
Loading…
Reference in a new issue