diff --git a/src/corelib/tools/qset.h b/src/corelib/tools/qset.h index 6eaeb8fc417..30549840d02 100644 --- a/src/corelib/tools/qset.h +++ b/src/corelib/tools/qset.h @@ -37,15 +37,18 @@ public: inline void swap(QSet &other) noexcept { q_hash.swap(other.q_hash); } #ifndef Q_QDOC - template - QTypeTraits::compare_eq_result_container operator==(const QSet &other) const - { return q_hash == other.q_hash; } - template - QTypeTraits::compare_eq_result_container operator!=(const QSet &other) const - { return q_hash != other.q_hash; } +private: + template = true> + friend bool comparesEqual(const QSet &lhs, const QSet &rhs) noexcept + { + return lhs.q_hash == rhs.q_hash; + } + QT_DECLARE_EQUALITY_OPERATORS_HELPER(QSet, QSet, /* non-constexpr */, noexcept, + template = true>) +public: #else - bool operator==(const QSet &other) const; - bool operator!=(const QSet &other) const; + friend bool operator==(const QSet &lhs, const QSet &rhs) noexcept; + friend bool operator!=(const QSet &lhs, const QSet &rhs) noexcept; #endif inline qsizetype size() const { return q_hash.size(); } diff --git a/src/corelib/tools/qset.qdoc b/src/corelib/tools/qset.qdoc index 4ef7a80a52d..19a21a6d32e 100644 --- a/src/corelib/tools/qset.qdoc +++ b/src/corelib/tools/qset.qdoc @@ -5,6 +5,7 @@ \class QSet \inmodule QtCore \brief The QSet class is a template class that provides a hash-table-based set. + \compares equality \ingroup tools \ingroup shared @@ -105,9 +106,9 @@ */ /*! - \fn template bool QSet::operator==(const QSet &other) const + \fn template bool QSet::operator==(const QSet lhs, const QSet &rhs) - Returns \c true if the \a other set is equal to this set; otherwise + Returns \c true if the \a lhs set is equal to the \a rhs set; otherwise returns \c false. Two sets are considered equal if they contain the same elements. @@ -118,9 +119,9 @@ */ /*! - \fn template bool QSet::operator!=(const QSet &other) const + \fn template bool QSet::operator!=(const QSet lhs, const QSet &rhs) - Returns \c true if the \a other set is not equal to this set; otherwise + Returns \c true if the \a lhs set is not equal to the \a rhs set; otherwise returns \c false. Two sets are considered equal if they contain the same elements. diff --git a/tests/auto/corelib/tools/qset/CMakeLists.txt b/tests/auto/corelib/tools/qset/CMakeLists.txt index 9e3e33ee7cb..e0b7c4d74e2 100644 --- a/tests/auto/corelib/tools/qset/CMakeLists.txt +++ b/tests/auto/corelib/tools/qset/CMakeLists.txt @@ -14,6 +14,8 @@ endif() qt_internal_add_test(tst_qset SOURCES tst_qset.cpp + LIBRARIES + Qt::TestPrivate ) qt_internal_undefine_global_definition(tst_qset QT_NO_JAVA_STYLE_ITERATORS) diff --git a/tests/auto/corelib/tools/qset/tst_qset.cpp b/tests/auto/corelib/tools/qset/tst_qset.cpp index 116d38112be..07d182b9b3f 100644 --- a/tests/auto/corelib/tools/qset/tst_qset.cpp +++ b/tests/auto/corelib/tools/qset/tst_qset.cpp @@ -5,6 +5,8 @@ #include #include +#include + int toNumber(const QString &str) { int res = 0; @@ -18,6 +20,7 @@ class tst_QSet : public QObject Q_OBJECT private slots: + void comparisonCompiles(); void operator_eq(); void swap(); void size(); @@ -56,50 +59,66 @@ struct IdentityTracker { inline size_t qHash(IdentityTracker key) { return qHash(key.value); } inline bool operator==(IdentityTracker lhs, IdentityTracker rhs) { return lhs.value == rhs.value; } +void tst_QSet::comparisonCompiles() +{ + QTestPrivate::testEqualityOperatorsCompile>(); + QTestPrivate::testEqualityOperatorsCompile>(); +} + void tst_QSet::operator_eq() { { QSet set1, set2; QVERIFY(set1 == set2); QVERIFY(!(set1 != set2)); + QT_TEST_EQUALITY_OPS(set1, set2, true); set1.insert(1); QVERIFY(set1 != set2); QVERIFY(!(set1 == set2)); + QT_TEST_EQUALITY_OPS(set1, set2, false); set2.insert(1); QVERIFY(set1 == set2); QVERIFY(!(set1 != set2)); + QT_TEST_EQUALITY_OPS(set1, set2, true); set2.insert(1); QVERIFY(set1 == set2); QVERIFY(!(set1 != set2)); + QT_TEST_EQUALITY_OPS(set1, set2, true); set1.insert(2); QVERIFY(set1 != set2); QVERIFY(!(set1 == set2)); + QT_TEST_EQUALITY_OPS(set1, set2, false); } { QSet set1, set2; QVERIFY(set1 == set2); QVERIFY(!(set1 != set2)); + QT_TEST_EQUALITY_OPS(set1, set2, true); set1.insert("one"); QVERIFY(set1 != set2); QVERIFY(!(set1 == set2)); + QT_TEST_EQUALITY_OPS(set1, set2, false); set2.insert("one"); QVERIFY(set1 == set2); QVERIFY(!(set1 != set2)); + QT_TEST_EQUALITY_OPS(set1, set2, true); set2.insert("one"); QVERIFY(set1 == set2); QVERIFY(!(set1 != set2)); + QT_TEST_EQUALITY_OPS(set1, set2, true); set1.insert("two"); QVERIFY(set1 != set2); QVERIFY(!(set1 == set2)); + QT_TEST_EQUALITY_OPS(set1, set2, false); } { @@ -111,6 +130,7 @@ void tst_QSet::operator_eq() QVERIFY(a != b); QVERIFY(!(a == b)); + QT_TEST_EQUALITY_OPS(a, b, false); } { @@ -118,9 +138,11 @@ void tst_QSet::operator_eq() s1.reserve(100); s2.reserve(4); QVERIFY(s1 == s2); + QT_TEST_EQUALITY_OPS(s1, s2, true); s1 << 100 << 200 << 300 << 400; s2 << 400 << 300 << 200 << 100; QVERIFY(s1 == s2); + QT_TEST_EQUALITY_OPS(s1, s2, true); } }