From 230bc059ae50a16346700a47090dcb97d239e8c0 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 8 Feb 2023 10:41:09 +0100 Subject: QVarLengthArray: use new q20::construct_at instead of raw placement new MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: MÃ¥rten Nordheim --- src/corelib/tools/qvarlengtharray.h | 18 +++++++++--------- 1 file 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 #include #include -#include +#include #include #include @@ -187,7 +187,7 @@ protected: { if (size() == capacity()) // ie. size() != 0 growBy(prealloc, array, 1); - reference r = *new (end()) T(std::forward(args)...); + reference r = *q20::construct_at(end(), std::forward(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::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::QVarLengthArray(qsizetype asize) if constexpr (QTypeInfo::isComplex) { T *i = end(); while (i != begin()) - new (--i) T; + q20::construct_at(--i); } } @@ -909,17 +909,17 @@ Q_OUTOFLINE_TEMPLATE auto QVLABase::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)...); } else { - new (b) T(std::forward(args)...); + q20::construct_at(b, std::forward(args)...); } } else { T *b = begin() + offset; memmove(static_cast(b + 1), static_cast(b), (size() - offset) * sizeof(T)); - new (b) T(std::forward(args)...); + q20::construct_at(b, std::forward(args)...); } this->s += 1; return data() + offset; @@ -948,7 +948,7 @@ Q_OUTOFLINE_TEMPLATE auto QVLABase::insert_impl(qsizetype prealloc, void *arr T *i = b + n; memmove(static_cast(i), static_cast(b), (size() - offset - n) * sizeof(T)); while (i != b) - new (--i) T(copy); + q20::construct_at(--i, copy); } } return data() + offset; -- cgit v1.2.3