diff options
author | Fawzi Mohamed <fawzi.mohamed@qt.io> | 2020-09-11 15:08:26 +0200 |
---|---|---|
committer | Fawzi Mohamed <fawzi.mohamed@qt.io> | 2020-10-08 18:13:34 +0200 |
commit | f69144471bd2e6a6b92f57e7727f28af9386d846 (patch) | |
tree | 81fd3633ceced7b25fc3c70f317924e631136d84 /src/corelib/kernel | |
parent | 6c7ed4c013393ce414f02fe4c9a88ebebeb3e47d (diff) |
Allow getting a const pointer out of a variant containing pointer
Currently
A a;
QVariant::fromValue(&a).value<const A*>() == nullptr;
Still casting non const to const is safe, and worked in Qt5.
After this change
A a;
QVariant::fromValue(&a).value<const A*>() == &a;
Change-Id: I257049d084c712b00a338a2943d379aa478e0981
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/corelib/kernel')
-rw-r--r-- | src/corelib/kernel/qvariant.h | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/src/corelib/kernel/qvariant.h b/src/corelib/kernel/qvariant.h index 04b93c262a..907094d25c 100644 --- a/src/corelib/kernel/qvariant.h +++ b/src/corelib/kernel/qvariant.h @@ -53,6 +53,7 @@ #include <QtCore/qbytearraylist.h> #endif #include <memory> +#include <type_traits> #if __has_include(<variant>) && __cplusplus >= 201703L #include <variant> @@ -572,6 +573,12 @@ template<typename T> inline T qvariant_cast(const QVariant &v) QMetaType targetType = QMetaType::fromType<T>(); if (v.d.type() == targetType) return v.d.get<T>(); + if constexpr (std::is_same_v<T,std::remove_const_t<std::remove_pointer_t<T>> const *>) { + using nonConstT = std::remove_const_t<std::remove_pointer_t<T>> *; + QMetaType nonConstTargetType = QMetaType::fromType<nonConstT>(); + if (v.d.type() == nonConstTargetType) + return v.d.get<nonConstT>(); + } T t{}; QMetaType::convert(v.metaType(), v.constData(), targetType, &t); |