From 82743fb8a237c4b2aad9f3c029de4da73379f352 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 8 Jun 2020 09:34:48 +0200 Subject: [PATCH] QPair: add a check that SB works as expected We have no doubt it does, because it's compiler-synthesized, but we might want to implement the tuple protocol for QPair in the future and then this will act as a safety net, emulating what users are currently already doing with QPair. Change-Id: Ie37f0214bb1aa64210d25be8a256606f4572febe Reviewed-by: Thiago Macieira --- tests/auto/corelib/tools/qpair/tst_qpair.cpp | 51 ++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tests/auto/corelib/tools/qpair/tst_qpair.cpp b/tests/auto/corelib/tools/qpair/tst_qpair.cpp index 8eeddf5320..0708c15530 100644 --- a/tests/auto/corelib/tools/qpair/tst_qpair.cpp +++ b/tests/auto/corelib/tools/qpair/tst_qpair.cpp @@ -36,6 +36,7 @@ class tst_QPair : public QObject Q_OBJECT private Q_SLOTS: void pairOfReferences(); + void structuredBindings(); void testConstexpr(); void testConversions(); void taskQTBUG_48780_pairContainingCArray(); @@ -121,6 +122,56 @@ void tst_QPair::pairOfReferences() QCOMPARE(p.second, QLatin1String("World")); } +void tst_QPair::structuredBindings() +{ + using PV = QPair; + using PR = QPair; + + { + PV pv = {42, "Hello"}; + PR pr = {pv.first, pv.second}; + + auto [fv, sv] = pv; + + fv = 24; + sv = "World"; + QCOMPARE(fv, 24); + QCOMPARE(sv, "World"); + QCOMPARE(pv.first, 42); + QCOMPARE(pv.second, "Hello"); + + auto [fr, sr] = pr; + + fr = 2424; + // sr = "World"; // const + QCOMPARE(fr, 2424); + QCOMPARE(pv.first, 2424); + } + + { + PV pv = {42, "Hello"}; + PR pr = {pv.first, pv.second}; + + auto& [fv, sv] = pv; + + fv = 24; + sv = "World"; + QCOMPARE(fv, 24); + QCOMPARE(sv, "World"); + QCOMPARE(pv.first, 24); + QCOMPARE(pv.second, "World"); + + auto& [fr, sr] = pr; + + fr = 4242; + //sr = "2World"; // const + + QCOMPARE(fr, 4242); + QCOMPARE(pr.first, 4242); + QCOMPARE(pv.first, 4242); + } +} + void tst_QPair::testConstexpr() { Q_CONSTEXPR QPair pID = qMakePair(0, 0.0);