diff options
author | Marc Mutz <marc.mutz@qt.io> | 2023-02-08 10:41:09 +0100 |
---|---|---|
committer | Dennis Oberst <dennis.oberst@qt.io> | 2023-02-09 05:01:09 +0100 |
commit | 230bc059ae50a16346700a47090dcb97d239e8c0 (patch) | |
tree | 8cbb03ab981fb7a3c1565604bff6ca0ee2b2b92e | |
parent | ed26352a1009ce37fe792c023af9e5d6259ea6d4 (diff) |
QVarLengthArray: use new q20::construct_at instead of raw placement new
The former is much safer to use in the presence of overloaded operator
new, and forms a tiny step towards a fully C++20-constexpr QVLA in the
future.
Pick-to: 6.5
Change-Id: If1b31c06296a60c39f1c9f9da523e208ecb1248b
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
-rw-r--r-- | src/corelib/tools/qvarlengtharray.h | 18 |
1 files changed, 9 insertions, 9 deletions
diff --git a/src/corelib/tools/qvarlengtharray.h b/src/corelib/tools/qvarlengtharray.h index f573855598..ac7e8ccc10 100644 --- a/src/corelib/tools/qvarlengtharray.h +++ b/src/corelib/tools/qvarlengtharray.h @@ -18,7 +18,7 @@ #include <algorithm> #include <initializer_list> #include <iterator> -#include <memory> +#include <QtCore/q20memory.h> #include <new> #include <string.h> @@ -187,7 +187,7 @@ protected: { if (size() == capacity()) // ie. size() != 0 growBy(prealloc, array, 1); - reference r = *new (end()) T(std::forward<Args>(args)...); + reference r = *q20::construct_at(end(), std::forward<Args>(args)...); ++s; return r; } @@ -213,7 +213,7 @@ protected: { reallocate_impl(prealloc, array, sz, qMax(sz, capacity())); while (size() < sz) { - new (data() + size()) T(v); + q20::construct_at(data() + size(), v); ++s; } } @@ -223,7 +223,7 @@ protected: if constexpr (QTypeInfo<T>::isComplex) { // call default constructor for new objects (which can throw) while (size() < sz) { - new (data() + size()) T; + q20::construct_at(data() + size()); ++s; } } else { @@ -674,7 +674,7 @@ Q_INLINE_TEMPLATE QVarLengthArray<T, Prealloc>::QVarLengthArray(qsizetype asize) if constexpr (QTypeInfo<T>::isComplex) { T *i = end(); while (i != begin()) - new (--i) T; + q20::construct_at(--i); } } @@ -909,17 +909,17 @@ Q_OUTOFLINE_TEMPLATE auto QVLABase<T>::emplace_impl(qsizetype prealloc, void *ar T *j = i + 1; // The new end-element needs to be constructed, the rest must be move assigned if (i != b) { - new (--j) T(std::move(*--i)); + q20::construct_at(--j, std::move(*--i)); while (i != b) *--j = std::move(*--i); *b = T(std::forward<Args>(args)...); } else { - new (b) T(std::forward<Args>(args)...); + q20::construct_at(b, std::forward<Args>(args)...); } } else { T *b = begin() + offset; memmove(static_cast<void *>(b + 1), static_cast<const void *>(b), (size() - offset) * sizeof(T)); - new (b) T(std::forward<Args>(args)...); + q20::construct_at(b, std::forward<Args>(args)...); } this->s += 1; return data() + offset; @@ -948,7 +948,7 @@ Q_OUTOFLINE_TEMPLATE auto QVLABase<T>::insert_impl(qsizetype prealloc, void *arr T *i = b + n; memmove(static_cast<void *>(i), static_cast<const void *>(b), (size() - offset - n) * sizeof(T)); while (i != b) - new (--i) T(copy); + q20::construct_at(--i, copy); } } return data() + offset; |