aboutsummaryrefslogtreecommitdiff
path: root/src/prevector.h
diff options
context:
space:
mode:
authorMartin Leitner-Ankerl <martin.ankerl@gmail.com>2022-04-24 12:55:57 +0200
committerMartin Leitner-Ankerl <martin.ankerl@gmail.com>2022-04-24 20:02:04 +0200
commit11e79084845a78e2421ea3abafe0de5a54ca2bde (patch)
tree7711f4e75ae5ef2cf11da73a257f87629f32cc7a /src/prevector.h
parentb1c5991eebb916755be188f355ad36fe01a3f529 (diff)
downloadbitcoin-11e79084845a78e2421ea3abafe0de5a54ca2bde.tar.xz
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.
Diffstat (limited to 'src/prevector.h')
-rw-r--r--src/prevector.h15
1 files changed, 3 insertions, 12 deletions
diff --git a/src/prevector.h b/src/prevector.h
index aa20efaaa7..fe8339eb8b 100644
--- a/src/prevector.h
+++ b/src/prevector.h
@@ -35,6 +35,8 @@
*/
template<unsigned int N, typename T, typename Size = uint32_t, typename Diff = int32_t>
class prevector {
+ static_assert(std::is_trivially_copyable_v<T>);
+
public:
typedef Size size_type;
typedef Diff difference_type;
@@ -411,15 +413,7 @@ public:
// representation (with capacity N and size <= N).
iterator p = first;
char* endp = (char*)&(*end());
- if (!std::is_trivially_destructible<T>::value) {
- while (p != last) {
- (*p).~T();
- _size--;
- ++p;
- }
- } else {
- _size -= last - p;
- }
+ _size -= last - p;
memmove(&(*first), &(*last), endp - ((char*)(&(*last))));
return first;
}
@@ -464,9 +458,6 @@ public:
}
~prevector() {
- if (!std::is_trivially_destructible<T>::value) {
- clear();
- }
if (!is_direct()) {
free(_union.indirect_contents.indirect);
_union.indirect_contents.indirect = nullptr;