From 98ca31981977c5a265bb745cc70aafd17384d346 Mon Sep 17 00:00:00 2001 From: Fabian Kosmale Date: Fri, 28 Feb 2020 15:27:53 +0100 Subject: [PATCH] QMetaType::fromType: support classes with inaccessible dtors Change-Id: I60a1b2496d48651a8166173b420da37f59d7a395 Reviewed-by: Lars Knoll --- src/corelib/kernel/qmetatype.h | 5 ++++- .../corelib/kernel/qmetatype/tst_qmetatype.cpp | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/corelib/kernel/qmetatype.h b/src/corelib/kernel/qmetatype.h index 0bec7ca4c0..1ae0fb45bc 100644 --- a/src/corelib/kernel/qmetatype.h +++ b/src/corelib/kernel/qmetatype.h @@ -2719,7 +2719,10 @@ QMetaTypeInterface QMetaTypeForType::metaType = { } }), /*.dtor=*/ QT_METATYPE_CONSTEXPRLAMDA( -> QMetaTypeInterface::DtorFn { - return [](const QMetaTypeInterface *, void *addr) { reinterpret_cast(addr)->~T(); }; + if constexpr (std::is_destructible_v) + return [](const QMetaTypeInterface *, void *addr) { reinterpret_cast(addr)->~T(); }; + else + return nullptr; }), /*.legacyRegisterOp=*/ QT_METATYPE_CONSTEXPRLAMDA( -> QMetaTypeInterface::LegacyRegisterOp { if constexpr (QMetaTypeId2::Defined && !QMetaTypeId2::IsBuiltIn) { diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp index 0d9dfc1c4c..f144aad7e4 100644 --- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp +++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp @@ -126,6 +126,7 @@ private slots: void fromType(); void operatorEq_data(); void operatorEq(); + void typesWithInaccessibleDTors(); }; struct BaseGenericType @@ -2574,6 +2575,21 @@ void tst_QMetaType::operatorEq() QCOMPARE(typeB != typeA, !eq); } +class WithPrivateDTor { + ~WithPrivateDTor(){}; +}; + +struct WithDeletedDtor { + ~WithDeletedDtor() = delete; +}; + +void tst_QMetaType::typesWithInaccessibleDTors() +{ + // should compile + Q_UNUSED(QMetaType::fromType()); + Q_UNUSED(QMetaType::fromType()); +} + // Compile-time test, it should be possible to register function pointer types class Undefined;