Skip to content

Commit 45a5aaf

Browse files
committed
Only call clear on prevector if it isn't trivially destructible and don't loop in clear
1 parent aaa02e7 commit 45a5aaf

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/prevector.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <string.h>
1111

1212
#include <iterator>
13+
#include <type_traits>
1314

1415
#pragma pack(push, 1)
1516
/** Implements a drop-in replacement for std::vector<T> which stores up to N
@@ -382,10 +383,14 @@ class prevector {
382383
iterator erase(iterator first, iterator last) {
383384
iterator p = first;
384385
char* endp = (char*)&(*end());
385-
while (p != last) {
386-
(*p).~T();
387-
_size--;
388-
++p;
386+
if (!std::is_trivially_destructible<T>::value) {
387+
while (p != last) {
388+
(*p).~T();
389+
_size--;
390+
++p;
391+
}
392+
} else {
393+
_size -= last - p;
389394
}
390395
memmove(&(*first), &(*last), endp - ((char*)(&(*last))));
391396
return first;
@@ -426,7 +431,9 @@ class prevector {
426431
}
427432

428433
~prevector() {
429-
clear();
434+
if (!std::is_trivially_destructible<T>::value) {
435+
clear();
436+
}
430437
if (!is_direct()) {
431438
free(_union.indirect);
432439
_union.indirect = NULL;

0 commit comments

Comments
 (0)