tst_qvarlengtharray: fix MyBase trackers for swap()

I don't begin to understand the semantics of the trackers here, but
whatever they are, they break with the fallback std::swap() 3-moves
implementation and lose track of alive objects, so provide an ADL swap
that does the right thing.

Amends dd58ddd5d9 (I think).

Pick-to: 6.5 6.4 6.2 5.15
Change-Id: I1cd49c95dca2d103a26c2c7ac0a896929135a6c8
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Marc Mutz 2023-02-10 11:02:11 +01:00
parent bb42e8e2c6
commit 49fca96d88

View File

@ -590,6 +590,12 @@ struct MyBase
bool hasMoved() const { return !wasConstructedAt(this); }
protected:
void swap(MyBase &other) {
using std::swap;
swap(data, other.data);
swap(isCopy, other.isCopy);
}
MyBase(const MyBase *data, bool isCopy)
: data(data), isCopy(isCopy) {}
@ -664,6 +670,14 @@ struct MyMovable
return *this;
}
void swap(MyMovable &other) noexcept
{
MyBase::swap(other);
std::swap(i, other.i);
}
friend void swap(MyMovable &lhs, MyMovable &rhs) noexcept { lhs.swap(rhs); }
bool operator==(const MyMovable &other) const
{
return i == other.i;
@ -679,6 +693,15 @@ struct MyComplex
{
return i == other.i;
}
void swap(MyComplex &other) noexcept
{
MyBase::swap(other);
std::swap(i, other.i);
}
friend void swap(MyComplex &lhs, MyComplex &rhs) noexcept { lhs.swap(rhs); }
char i;
};
@ -1310,6 +1333,17 @@ void tst_QVarLengthArray::insertMove()
QCOMPARE(MyBase::liveCount, 0);
QCOMPARE(MyBase::copyCount, 0);
{
MyMovable m1, m2;
QCOMPARE(MyBase::liveCount, 2);
QCOMPARE(MyBase::copyCount, 0);
using std::swap;
swap(m1, m2);
QCOMPARE(MyBase::liveCount, 2);
QCOMPARE(MyBase::movedCount, 0);
QCOMPARE(MyBase::copyCount, 0);
}
{
QVarLengthArray<MyMovable, 6> vec;
MyMovable m1;