Make QVarLengthArray comparisons hidden friends

Task-number: QTBUG-87975
Change-Id: Iaebb237b3d5d3e881caf9a93153e295af051e2ab
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
bb10
Allan Sandfeld Jensen 2020-10-29 17:02:06 +01:00
parent b0d4d95a29
commit 951274a9b9
2 changed files with 80 additions and 43 deletions

View File

@ -310,6 +310,68 @@ public:
inline const T &back() const { return last(); }
void shrink_to_fit() { squeeze(); }
#ifdef Q_QDOC
template <typename T, qsizetype Prealloc1, qsizetype Prealloc2>
friend inline bool operator==(const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r);
template <typename T, qsizetype Prealloc1, qsizetype Prealloc2>
friend inline bool operator!=(const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r);
template <typename T, qsizetype Prealloc1, qsizetype Prealloc2>
friend inline bool operator< (const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r);
template <typename T, qsizetype Prealloc1, qsizetype Prealloc2>
friend inline bool operator> (const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r);
template <typename T, qsizetype Prealloc1, qsizetype Prealloc2>
friend inline bool operator<=(const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r);
template <typename T, qsizetype Prealloc1, qsizetype Prealloc2>
friend inline bool operator>=(const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r);
#else
template <typename U = T, qsizetype Prealloc2 = Prealloc> friend
QTypeTraits::compare_eq_result<U> operator==(const QVarLengthArray<T, Prealloc> &l, const QVarLengthArray<T, Prealloc2> &r)
{
if (l.size() != r.size())
return false;
const T *rb = r.begin();
const T *b = l.begin();
const T *e = l.end();
return std::equal(b, e, QT_MAKE_CHECKED_ARRAY_ITERATOR(rb, r.size()));
}
template <typename U = T, qsizetype Prealloc2 = Prealloc> friend
QTypeTraits::compare_eq_result<U> operator!=(const QVarLengthArray<T, Prealloc> &l, const QVarLengthArray<T, Prealloc2> &r)
{
return !(l == r);
}
template <typename U = T, qsizetype Prealloc2 = Prealloc> friend
QTypeTraits::compare_lt_result<U> operator<(const QVarLengthArray<T, Prealloc> &lhs, const QVarLengthArray<T, Prealloc2> &rhs)
noexcept(noexcept(std::lexicographical_compare(lhs.begin(), lhs.end(),
rhs.begin(), rhs.end())))
{
return std::lexicographical_compare(lhs.begin(), lhs.end(),
rhs.begin(), rhs.end());
}
template <typename U = T, qsizetype Prealloc2 = Prealloc> friend
QTypeTraits::compare_lt_result<U> operator>(const QVarLengthArray<T, Prealloc> &lhs, const QVarLengthArray<T, Prealloc2> &rhs)
noexcept(noexcept(lhs < rhs))
{
return rhs < lhs;
}
template <typename U = T, qsizetype Prealloc2 = Prealloc> friend
QTypeTraits::compare_lt_result<U> operator<=(const QVarLengthArray<T, Prealloc> &lhs, const QVarLengthArray<T, Prealloc2> &rhs)
noexcept(noexcept(lhs < rhs))
{
return !(lhs > rhs);
}
template <typename U = T, qsizetype Prealloc2 = Prealloc> friend
QTypeTraits::compare_lt_result<U> operator>=(const QVarLengthArray<T, Prealloc> &lhs, const QVarLengthArray<T, Prealloc2> &rhs)
noexcept(noexcept(lhs < rhs))
{
return !(lhs < rhs);
}
#endif
private:
void reallocate(qsizetype size, qsizetype alloc);
@ -626,52 +688,27 @@ Q_OUTOFLINE_TEMPLATE typename QVarLengthArray<T, Prealloc>::iterator QVarLengthA
return ptr + f;
}
#ifdef Q_QDOC
// Fake definitions for qdoc, only the redeclaration is used.
template <typename T, qsizetype Prealloc1, qsizetype Prealloc2>
QTypeTraits::compare_eq_result<T> operator==(const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r)
{
if (l.size() != r.size())
return false;
const T *rb = r.begin();
const T *b = l.begin();
const T *e = l.end();
return std::equal(b, e, QT_MAKE_CHECKED_ARRAY_ITERATOR(rb, r.size()));
}
bool operator==(const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r)
{ return bool{}; }
template <typename T, qsizetype Prealloc1, qsizetype Prealloc2>
QTypeTraits::compare_eq_result<T> operator!=(const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r)
{
return !(l == r);
}
bool operator!=(const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r)
{ return bool{}; }
template <typename T, qsizetype Prealloc1, qsizetype Prealloc2>
QTypeTraits::compare_lt_result<T> operator<(const QVarLengthArray<T, Prealloc1> &lhs, const QVarLengthArray<T, Prealloc2> &rhs)
noexcept(noexcept(std::lexicographical_compare(lhs.begin(), lhs.end(),
rhs.begin(), rhs.end())))
{
return std::lexicographical_compare(lhs.begin(), lhs.end(),
rhs.begin(), rhs.end());
}
bool operator< (const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r)
{ return bool{}; }
template <typename T, qsizetype Prealloc1, qsizetype Prealloc2>
QTypeTraits::compare_lt_result<T> operator>(const QVarLengthArray<T, Prealloc1> &lhs, const QVarLengthArray<T, Prealloc2> &rhs)
noexcept(noexcept(lhs < rhs))
{
return rhs < lhs;
}
bool operator> (const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r)
{ return bool{}; }
template <typename T, qsizetype Prealloc1, qsizetype Prealloc2>
QTypeTraits::compare_lt_result<T> operator<=(const QVarLengthArray<T, Prealloc1> &lhs, const QVarLengthArray<T, Prealloc2> &rhs)
noexcept(noexcept(lhs < rhs))
{
return !(lhs > rhs);
}
bool operator<=(const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r)
{ return bool{}; }
template <typename T, qsizetype Prealloc1, qsizetype Prealloc2>
QTypeTraits::compare_lt_result<T> operator>=(const QVarLengthArray<T, Prealloc1> &lhs, const QVarLengthArray<T, Prealloc2> &rhs)
noexcept(noexcept(lhs < rhs))
{
return !(lhs < rhs);
}
bool operator>=(const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r)
{ return bool{}; }
#endif
template <typename T, qsizetype Prealloc>
size_t qHash(const QVarLengthArray<T, Prealloc> &key, size_t seed = 0)

View File

@ -877,11 +877,11 @@ void tst_QVarLengthArray::squeeze()
void tst_QVarLengthArray::operators()
{
QVarLengthArray<QString> myvla;
QVarLengthArray<QString, 6> myvla;
myvla << "A" << "B" << "C";
QVarLengthArray<QString> myvlatwo;
QVarLengthArray<QString, 3> myvlatwo;
myvlatwo << "D" << "E" << "F";
QVarLengthArray<QString> combined;
QVarLengthArray<QString, 7> combined;
combined << "A" << "B" << "C" << "D" << "E" << "F";
// !=