summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/corelib/global/q20memory.h28
-rw-r--r--src/corelib/global/q20type_traits.h17
-rw-r--r--src/corelib/serialization/qdatastream.cpp4
3 files changed, 47 insertions, 2 deletions
diff --git a/src/corelib/global/q20memory.h b/src/corelib/global/q20memory.h
index d11725ce5ce..6dd4bf5afae 100644
--- a/src/corelib/global/q20memory.h
+++ b/src/corelib/global/q20memory.h
@@ -9,7 +9,7 @@
#include <QtCore/q17memory.h>
-#include <type_traits>
+#include <QtCore/q20type_traits.h>
#include <utility>
//
@@ -46,6 +46,32 @@ T *construct_at(T *ptr, Args && ... args)
#endif // __cpp_lib_constexpr_dynamic_alloc
} // namespace q20
+// like std::make_unique_for_overwrite (excl. C++23-added constexpr)
+namespace q20 {
+#ifdef __cpp_lib_smart_ptr_for_overwrite
+using std::make_unique_for_overwrite;
+#else
+// https://eel.is/c++draft/unique.ptr.create#6
+template <typename T>
+std::enable_if_t<!std::is_array_v<T>, std::unique_ptr<T>>
+make_unique_for_overwrite()
+// https://eel.is/c++draft/unique.ptr.create#7
+{ return std::unique_ptr<T>(new T); }
+
+// https://eel.is/c++draft/unique.ptr.create#8
+template <typename T>
+std::enable_if_t<q20::is_unbounded_array_v<T>, std::unique_ptr<T>>
+make_unique_for_overwrite(std::size_t n)
+// https://eel.is/c++draft/unique.ptr.create#9
+{ return std::unique_ptr<T>(new std::remove_extent_t<T>[n]); }
+
+// https://eel.is/c++draft/unique.ptr.create#10
+template <typename T, typename...Args>
+std::enable_if_t<q20::is_bounded_array_v<T>>
+make_unique_for_overwrite(Args&&...) = delete;
+
+#endif // __cpp_lib_smart_ptr_for_overwrite
+} // namespace q20
namespace q20 {
// like std::to_address
diff --git a/src/corelib/global/q20type_traits.h b/src/corelib/global/q20type_traits.h
index 63a453daca8..97614fbab6a 100644
--- a/src/corelib/global/q20type_traits.h
+++ b/src/corelib/global/q20type_traits.h
@@ -28,6 +28,23 @@
QT_BEGIN_NAMESPACE
namespace q20 {
+// like std::is_(un)bounded_array
+#ifdef __cpp_lib_bounded_array_traits
+using std::is_bounded_array;
+using std::is_bounded_array_v;
+using std::is_unbounded_array;
+using std::is_unbounded_array_v;
+#else
+template <typename T> struct is_bounded_array : std::false_type {};
+template <typename T, std::size_t N> struct is_bounded_array<T[N]> : std::true_type {};
+template <typename T> struct is_unbounded_array : std::false_type {};
+template <typename T> struct is_unbounded_array<T[]> : std::true_type {};
+template <typename T> constexpr inline bool is_bounded_array_v = q20::is_bounded_array<T>::value;
+template <typename T> constexpr inline bool is_unbounded_array_v = q20::is_unbounded_array<T>::value;
+#endif
+}
+
+namespace q20 {
// like std::is_constant_evaluated
#ifdef __cpp_lib_is_constant_evaluated
using std::is_constant_evaluated;
diff --git a/src/corelib/serialization/qdatastream.cpp b/src/corelib/serialization/qdatastream.cpp
index 039c8fbcdc3..f9c08cd78e4 100644
--- a/src/corelib/serialization/qdatastream.cpp
+++ b/src/corelib/serialization/qdatastream.cpp
@@ -13,6 +13,8 @@
#include <stdlib.h>
#include "qendian.h"
+#include <QtCore/q20memory.h>
+
QT_BEGIN_NAMESPACE
constexpr quint32 QDataStream::NullCode;
@@ -1096,7 +1098,7 @@ QDataStream &QDataStream::readBytes(char *&s, qint64 &l)
do {
qsizetype blockSize = qMin(step, len - allocated);
const qsizetype n = allocated + blockSize + 1;
- if (const auto prevBuf = std::exchange(curBuf, std::make_unique<char[]>(n)))
+ if (const auto prevBuf = std::exchange(curBuf, q20::make_unique_for_overwrite<char[]>(n)))
memcpy(curBuf.get(), prevBuf.get(), allocated);
if (readBlock(curBuf.get() + allocated, blockSize) != blockSize)
return *this;