summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFabian Kosmale <fabian.kosmale@qt.io>2021-01-14 23:23:37 +0100
committerFabian Kosmale <fabian.kosmale@qt.io>2021-01-18 15:51:18 +0100
commit2d8757f879d8f410319aae41ff902ba5a679d9dd (patch)
tree4ef80a0f475f6aeff3a7ae4b2c1ead34dc838a82
parentec12c302493e69deb9b3019ecb182068518691ac (diff)
QVariant::fromValue<T>: require T to be copy constructible
In Qt 5, QVariant::fromValue<T> 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 <lars.knoll@qt.io>
-rw-r--r--src/corelib/kernel/qvariant.h5
-rw-r--r--tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp15
2 files changed, 20 insertions, 0 deletions
diff --git a/src/corelib/kernel/qvariant.h b/src/corelib/kernel/qvariant.h
index 8c3a27e3ca..b7d347a61e 100644
--- a/src/corelib/kernel/qvariant.h
+++ b/src/corelib/kernel/qvariant.h
@@ -392,7 +392,12 @@ class Q_CORE_EXPORT QVariant
}
template<typename T>
+#ifndef Q_CLANG_QDOC
+ static inline auto fromValue(const T &value) ->
+ std::enable_if_t<std::is_copy_constructible_v<T>, QVariant>
+#else
static inline QVariant fromValue(const T &value)
+#endif
{
return QVariant(QMetaType::fromType<T>(), std::addressof(value));
}
diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp
index 6b30419bb8..23ee85ee46 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<typename T, typename = void>
+struct QVariantFromValueCompiles
+{
+ static inline constexpr bool value = false;
+};
+
+template<typename T>
+struct QVariantFromValueCompiles<T, std::void_t<decltype (QVariant::fromValue(std::declval<T>()))>>
+{
+ static inline constexpr bool value = true;
+};
+
+static_assert(QVariantFromValueCompiles<int>::value);
+static_assert(!QVariantFromValueCompiles<QObject>::value);
+
class tst_QVariant : public QObject
{
Q_OBJECT