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 dd58ddd5d97f0663d5fafb7e81bff4fc7db13ba7 (I think). Change-Id: I1cd49c95dca2d103a26c2c7ac0a896929135a6c8 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io> (cherry picked from commit 49fca96d88c308bc22cd898a8d202228d185654e) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
598be12d4a
commit
310d868d12
@ -590,6 +590,12 @@ struct MyBase
|
|||||||
bool hasMoved() const { return !wasConstructedAt(this); }
|
bool hasMoved() const { return !wasConstructedAt(this); }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
void swap(MyBase &other) {
|
||||||
|
using std::swap;
|
||||||
|
swap(data, other.data);
|
||||||
|
swap(isCopy, other.isCopy);
|
||||||
|
}
|
||||||
|
|
||||||
MyBase(const MyBase *data, bool isCopy)
|
MyBase(const MyBase *data, bool isCopy)
|
||||||
: data(data), isCopy(isCopy) {}
|
: data(data), isCopy(isCopy) {}
|
||||||
|
|
||||||
@ -664,6 +670,14 @@ struct MyMovable
|
|||||||
return *this;
|
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
|
bool operator==(const MyMovable &other) const
|
||||||
{
|
{
|
||||||
return i == other.i;
|
return i == other.i;
|
||||||
@ -679,6 +693,15 @@ struct MyComplex
|
|||||||
{
|
{
|
||||||
return i == other.i;
|
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;
|
char i;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1310,6 +1333,17 @@ void tst_QVarLengthArray::insertMove()
|
|||||||
QCOMPARE(MyBase::liveCount, 0);
|
QCOMPARE(MyBase::liveCount, 0);
|
||||||
QCOMPARE(MyBase::copyCount, 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;
|
QVarLengthArray<MyMovable, 6> vec;
|
||||||
MyMovable m1;
|
MyMovable m1;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user