diff --git a/src/corelib/kernel/qmetatype.h b/src/corelib/kernel/qmetatype.h index 59ec8de0e9..7c22ff1693 100644 --- a/src/corelib/kernel/qmetatype.h +++ b/src/corelib/kernel/qmetatype.h @@ -1014,6 +1014,24 @@ struct ContainerCapabilitiesImpl().p { static_cast(const_cast(container))->push_back(*static_cast(value)); } }; +namespace QtPrivate { +namespace ContainerCapabilitiesMetaProgrammingHelper { + template + using void_t = void; +} +} + +template +struct ContainerCapabilitiesImpl().insert(std::declval())), decltype(std::declval() == std::declval())>> +{ + enum {ContainerCapabilities = ContainerIsAppendable}; + + // The code below invokes undefined behavior if and only if the pointer passed into QSequentialIterableImpl + // pointed to a const object to begin with + static void appendImpl(const void *container, const void *value) + { static_cast(const_cast(container))->insert(*static_cast(value)); } +}; + template::iterator_category> struct CapabilitiesImpl; diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp index 70bda1a0ef..86c61cba12 100644 --- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp +++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp @@ -5161,14 +5161,28 @@ void tst_QVariant::sequentialIterableEndianessSanityCheck() void tst_QVariant::sequentialIterableAppend() { - QVector container {1, 2}; - auto variant = QVariant::fromValue(container); - QVERIFY(variant.canConvert()); - auto asIterable = variant.value(); - const int i = 3, j = 4; - asIterable.append(&i); - asIterable.append(&j); - QCOMPARE(variant.value>(), QVector ({1, 2, 3, 4})); + { + QVector container {1, 2}; + auto variant = QVariant::fromValue(container); + QVERIFY(variant.canConvert()); + auto asIterable = variant.value(); + const int i = 3, j = 4; + asIterable.append(&i); + asIterable.append(&j); + QCOMPARE(variant.value>(), QVector ({1, 2, 3, 4})); + } + { + QSet container { QByteArray{"hello"}, QByteArray{"world"} }; + auto variant = QVariant::fromValue(std::move(container)); + QVERIFY(variant.canConvert()); + auto asIterable = variant.value(); + QByteArray qba1 {"goodbye"}; + QByteArray qba2 { "moon" }; + asIterable.append( &qba1 ); + asIterable.append( &qba2); + QSet reference { "hello", "world", "goodbye", "moon" }; + QCOMPARE(variant.value>(), reference); + } } void tst_QVariant::preferDirectConversionOverInterfaces()