Skip to content

Commit 11e7908

Browse files
committed
prevector: only allow trivially copyable types
The prevector implementation currently can't be used with types that are not trivially copyable, due to the use of memmove. Trivially copyable implies that it is trivially destructible, see https://eel.is/c++draft/class.prop#1.3 That means that the checks for std::is_trivially_destructible are not necessary, and in fact where used it wouldn't be enough. E.g. in `erase(iterator, iterator)` the elements in range first-last are destructed, but it does not destruct elements left after `memmove`. This commit removes the checks for `std::is_trivially_destructible` and instead adds a `static_assert(std::is_trivially_copyable_v<T>);` to make sure `prevector` is only used with supported types.
1 parent b1c5991 commit 11e7908

File tree

1 file changed

+3
-12
lines changed

1 file changed

+3
-12
lines changed

src/prevector.h

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
*/
3636
template<unsigned int N, typename T, typename Size = uint32_t, typename Diff = int32_t>
3737
class prevector {
38+
static_assert(std::is_trivially_copyable_v<T>);
39+
3840
public:
3941
typedef Size size_type;
4042
typedef Diff difference_type;
@@ -411,15 +413,7 @@ class prevector {
411413
// representation (with capacity N and size <= N).
412414
iterator p = first;
413415
char* endp = (char*)&(*end());
414-
if (!std::is_trivially_destructible<T>::value) {
415-
while (p != last) {
416-
(*p).~T();
417-
_size--;
418-
++p;
419-
}
420-
} else {
421-
_size -= last - p;
422-
}
416+
_size -= last - p;
423417
memmove(&(*first), &(*last), endp - ((char*)(&(*last))));
424418
return first;
425419
}
@@ -464,9 +458,6 @@ class prevector {
464458
}
465459

466460
~prevector() {
467-
if (!std::is_trivially_destructible<T>::value) {
468-
clear();
469-
}
470461
if (!is_direct()) {
471462
free(_union.indirect_contents.indirect);
472463
_union.indirect_contents.indirect = nullptr;

0 commit comments

Comments
 (0)