diff --git a/src/corelib/tools/qvarlengtharray.h b/src/corelib/tools/qvarlengtharray.h index 54d97d8762..32f621c809 100644 --- a/src/corelib/tools/qvarlengtharray.h +++ b/src/corelib/tools/qvarlengtharray.h @@ -42,6 +42,9 @@ #include #include #include +#ifdef Q_COMPILER_INITIALIZER_LISTS +#include +#endif QT_BEGIN_NAMESPACE @@ -62,6 +65,14 @@ public: append(other.constData(), other.size()); } +#ifdef Q_COMPILER_INITIALIZER_LISTS + QVarLengthArray(std::initializer_list args) + : a(Prealloc), s(0), ptr(reinterpret_cast(array)) + { + append(args.begin(), args.size()); + } +#endif + inline ~QVarLengthArray() { if (QTypeInfo::isComplex) { T *i = ptr + s; diff --git a/src/corelib/tools/qvarlengtharray.qdoc b/src/corelib/tools/qvarlengtharray.qdoc index 6cc9f62c5a..d1b87f381e 100644 --- a/src/corelib/tools/qvarlengtharray.qdoc +++ b/src/corelib/tools/qvarlengtharray.qdoc @@ -100,6 +100,17 @@ \l{default-constructed value}. */ + +/*! \fn QVarLengthArray::QVarLengthArray(std::initializer_list args) + \since 5.5 + + Constructs an array from the std::initializer_list given by \a args. + + This constructor is only enabled if the compiler supports C++11 initializer + lists. +*/ + + /*! \fn QVarLengthArray::~QVarLengthArray() Destroys the array. diff --git a/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp b/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp index 0b507ca277..40917eebea 100644 --- a/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp +++ b/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp @@ -54,6 +54,12 @@ private slots: void indexOf(); void lastIndexOf(); void contains(); + void initializeListInt(); + void initializeListMovable(); + void initializeListComplex(); +private: + template + void initializeList(); }; int fooCtor = 0; @@ -333,11 +339,23 @@ struct MyPrimitive struct MyMovable : MyBase { + MyMovable(char input = 'j') : i(input) {} + bool operator==(const MyMovable &other) const + { + return i == other.i; + } + char i; }; struct MyComplex : MyBase { + MyComplex(char input = 'j') : i(input) {} + bool operator==(const MyComplex &other) const + { + return i == other.i; + } + char i; }; QT_BEGIN_NAMESPACE @@ -734,5 +752,49 @@ void tst_QVarLengthArray::contains() QVERIFY(myvec.contains(QLatin1String("I don't exist"))); } +void tst_QVarLengthArray::initializeListInt() +{ + initializeList(); +} + +void tst_QVarLengthArray::initializeListMovable() +{ + const int instancesCount = MyMovable::liveCount; + initializeList(); + QCOMPARE(MyMovable::liveCount, instancesCount); +} + +void tst_QVarLengthArray::initializeListComplex() +{ + const int instancesCount = MyComplex::liveCount; + initializeList(); + QCOMPARE(MyComplex::liveCount, instancesCount); +} + +template +void tst_QVarLengthArray::initializeList() +{ +#ifdef Q_COMPILER_INITIALIZER_LISTS + T val1(110); + T val2(105); + T val3(101); + T val4(114); + + QVarLengthArray v1 {val1, val2, val3}; + QCOMPARE(v1, QVarLengthArray() << val1 << val2 << val3); + QCOMPARE(v1, (QVarLengthArray {val1, val2, val3})); + + QVarLengthArray, 4> v2{ v1, {val4}, QVarLengthArray(), {val1, val2, val3} }; + QVarLengthArray, 4> v3; + v3 << v1 << (QVarLengthArray() << val4) << QVarLengthArray() << v1; + QCOMPARE(v3, v2); + + QVarLengthArray v4({}); + QCOMPARE(v4.size(), 0); +#else + QSKIP("This tests requires a compiler that supports initializer lists."); +#endif +} + QTEST_APPLESS_MAIN(tst_QVarLengthArray) #include "tst_qvarlengtharray.moc"