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. Change-Id: If1b31c06296a60c39f1c9f9da523e208ecb1248b Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> (cherry picked from commit 230bc059ae50a16346700a47090dcb97d239e8c0) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
ac88e82e98
commit
790a0e824d
@ -18,7 +18,7 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <initializer_list>
|
#include <initializer_list>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <memory>
|
#include <QtCore/q20memory.h>
|
||||||
#include <new>
|
#include <new>
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -194,7 +194,7 @@ protected:
|
|||||||
{
|
{
|
||||||
if (size() == capacity()) // ie. size() != 0
|
if (size() == capacity()) // ie. size() != 0
|
||||||
growBy(prealloc, array, 1);
|
growBy(prealloc, array, 1);
|
||||||
reference r = *new (end()) T(std::forward<Args>(args)...);
|
reference r = *q20::construct_at(end(), std::forward<Args>(args)...);
|
||||||
++s;
|
++s;
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
@ -220,7 +220,7 @@ protected:
|
|||||||
{
|
{
|
||||||
reallocate_impl(prealloc, array, sz, qMax(sz, capacity()));
|
reallocate_impl(prealloc, array, sz, qMax(sz, capacity()));
|
||||||
while (size() < sz) {
|
while (size() < sz) {
|
||||||
new (data() + size()) T(v);
|
q20::construct_at(data() + size(), v);
|
||||||
++s;
|
++s;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -230,7 +230,7 @@ protected:
|
|||||||
if constexpr (QTypeInfo<T>::isComplex) {
|
if constexpr (QTypeInfo<T>::isComplex) {
|
||||||
// call default constructor for new objects (which can throw)
|
// call default constructor for new objects (which can throw)
|
||||||
while (size() < sz) {
|
while (size() < sz) {
|
||||||
new (data() + size()) T;
|
q20::construct_at(data() + size());
|
||||||
++s;
|
++s;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -664,7 +664,7 @@ Q_INLINE_TEMPLATE QVarLengthArray<T, Prealloc>::QVarLengthArray(qsizetype asize)
|
|||||||
if constexpr (QTypeInfo<T>::isComplex) {
|
if constexpr (QTypeInfo<T>::isComplex) {
|
||||||
T *i = end();
|
T *i = end();
|
||||||
while (i != begin())
|
while (i != begin())
|
||||||
new (--i) T;
|
q20::construct_at(--i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -854,17 +854,17 @@ Q_OUTOFLINE_TEMPLATE auto QVLABase<T>::emplace_impl(qsizetype prealloc, void *ar
|
|||||||
T *j = i + 1;
|
T *j = i + 1;
|
||||||
// The new end-element needs to be constructed, the rest must be move assigned
|
// The new end-element needs to be constructed, the rest must be move assigned
|
||||||
if (i != b) {
|
if (i != b) {
|
||||||
new (--j) T(std::move(*--i));
|
q20::construct_at(--j, std::move(*--i));
|
||||||
while (i != b)
|
while (i != b)
|
||||||
*--j = std::move(*--i);
|
*--j = std::move(*--i);
|
||||||
*b = T(std::forward<Args>(args)...);
|
*b = T(std::forward<Args>(args)...);
|
||||||
} else {
|
} else {
|
||||||
new (b) T(std::forward<Args>(args)...);
|
q20::construct_at(b, std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
T *b = begin() + offset;
|
T *b = begin() + offset;
|
||||||
memmove(static_cast<void *>(b + 1), static_cast<const void *>(b), (size() - offset) * sizeof(T));
|
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;
|
this->s += 1;
|
||||||
return data() + offset;
|
return data() + offset;
|
||||||
@ -893,7 +893,7 @@ Q_OUTOFLINE_TEMPLATE auto QVLABase<T>::insert_impl(qsizetype prealloc, void *arr
|
|||||||
T *i = b + n;
|
T *i = b + n;
|
||||||
memmove(static_cast<void *>(i), static_cast<const void *>(b), (size() - offset - n) * sizeof(T));
|
memmove(static_cast<void *>(i), static_cast<const void *>(b), (size() - offset - n) * sizeof(T));
|
||||||
while (i != b)
|
while (i != b)
|
||||||
new (--i) T(copy);
|
q20::construct_at(--i, copy);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return data() + offset;
|
return data() + offset;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user