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>
This commit is contained in:
Fabian Kosmale 2021-01-14 23:23:37 +01:00
parent ec12c30249
commit 2d8757f879
2 changed files with 20 additions and 0 deletions

View File

@ -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));
}

View File

@ -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