QVariant: make customConstructShared() SCARY¹

Extract Method non-template customConstructSharedImpl() to avoid
instantiating std::unique_ptr with a different per-F Deleter over and
over again.

Not picking to 6.5 because the function was confined to the
qvariant.cpp TU in those versions.

Cf. 11791e2a50417661679f84aeae21ce959cab638f and
d783363f60173f1bc6525f1a8bbbd87f1e3afc1d for similar issues.

¹ https://www.open-std.org/jtc1/sc22/WG21/docs/papers/2009/n2911.pdf

Change-Id: I73d21d929a7db2ab47f62a3246cf913d82e3db75
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit 0ecf8a21587e33c5c5b554d5bc059488001c8990)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2023-05-31 19:33:10 +02:00 committed by Qt Cherry-pick Bot
parent 4f89eb5a7a
commit 96a67452b1

View File

@ -19,8 +19,7 @@
QT_BEGIN_NAMESPACE
template <typename F> static QVariant::PrivateShared *
customConstructShared(size_t size, size_t align, F &&construct)
inline auto customConstructSharedImpl(size_t size, size_t align)
{
struct Deleter {
void operator()(QVariant::PrivateShared *p) const
@ -30,6 +29,13 @@ customConstructShared(size_t size, size_t align, F &&construct)
// this is exception-safe
std::unique_ptr<QVariant::PrivateShared, Deleter> ptr;
ptr.reset(QVariant::PrivateShared::create(size, align));
return ptr;
}
template <typename F> static QVariant::PrivateShared *
customConstructShared(size_t size, size_t align, F &&construct)
{
auto ptr = customConstructSharedImpl(size, align);
construct(ptr->data());
return ptr.release();
}