From 2d8757f879d8f410319aae41ff902ba5a679d9dd Mon Sep 17 00:00:00 2001 From: Fabian Kosmale Date: Thu, 14 Jan 2021 23:23:37 +0100 Subject: [PATCH] QVariant::fromValue: require T to be copy constructible In Qt 5, QVariant::fromValue would not compile unless Q_DECLARE_METATYPE(T) was used, and Q_DECLARE_METATYPE(T) would lead to a compile error if T were not copy constructible. In Qt 6, we do not require Q_DECLARE_METATYPE before using fromValue, and QMetaType itself works with non-copy constructible types just fine. However, QVariant still requires it, thus we need to now enforce this in fromValue itself. Change-Id: Ib6964a438d8c46033dd3a037b9d871de2b42e175 Reviewed-by: Lars Knoll --- src/corelib/kernel/qvariant.h | 5 +++++ .../auto/corelib/kernel/qvariant/tst_qvariant.cpp | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/corelib/kernel/qvariant.h b/src/corelib/kernel/qvariant.h index 8c3a27e3ca3..b7d347a61e4 100644 --- a/src/corelib/kernel/qvariant.h +++ b/src/corelib/kernel/qvariant.h @@ -392,7 +392,12 @@ class Q_CORE_EXPORT QVariant } template +#ifndef Q_CLANG_QDOC + static inline auto fromValue(const T &value) -> + std::enable_if_t, QVariant> +#else static inline QVariant fromValue(const T &value) +#endif { return QVariant(QMetaType::fromType(), std::addressof(value)); } diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp index 6b30419bb83..23ee85ee46d 100644 --- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp +++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp @@ -68,6 +68,21 @@ class CustomNonQObject; +template +struct QVariantFromValueCompiles +{ + static inline constexpr bool value = false; +}; + +template +struct QVariantFromValueCompiles()))>> +{ + static inline constexpr bool value = true; +}; + +static_assert(QVariantFromValueCompiles::value); +static_assert(!QVariantFromValueCompiles::value); + class tst_QVariant : public QObject { Q_OBJECT