diff --git a/src/corelib/serialization/qjsonvalue.h b/src/corelib/serialization/qjsonvalue.h index fe7d0baf2f..7259bd1b7b 100644 --- a/src/corelib/serialization/qjsonvalue.h +++ b/src/corelib/serialization/qjsonvalue.h @@ -205,6 +205,11 @@ private: Q_DECLARE_SHARED(QJsonValue) +inline bool operator==(const QJsonValueRef &lhs, const QJsonValueRef &rhs) +{ return QJsonValue(lhs) == QJsonValue(rhs); } +inline bool operator!=(const QJsonValueRef &lhs, const QJsonValueRef &rhs) +{ return !(lhs == rhs); } + Q_CORE_EXPORT size_t qHash(const QJsonValue &value, size_t seed = 0); #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY) diff --git a/tests/auto/corelib/serialization/json/tst_qtjson.cpp b/tests/auto/corelib/serialization/json/tst_qtjson.cpp index 72dfc4a47d..7cb7bac83d 100644 --- a/tests/auto/corelib/serialization/json/tst_qtjson.cpp +++ b/tests/auto/corelib/serialization/json/tst_qtjson.cpp @@ -73,6 +73,7 @@ private Q_SLOTS: void testObjectNestedEmpty(); void testValueRef(); + void testValueRefComparison(); void testObjectIteration(); void testArrayIteration(); @@ -954,6 +955,57 @@ void tst_QtJson::testValueRef() QCOMPARE(object.value(QLatin1String("key")), QJsonValue(42)); } +void tst_QtJson::testValueRefComparison() +{ + QJsonValue a0 = 42.; + QJsonValue a1 = QStringLiteral("142"); + +#define CHECK_IMPL(lhs, rhs, ineq) \ + QCOMPARE(lhs, rhs); \ + QVERIFY(!(lhs != rhs)); \ + QVERIFY(lhs != ineq); \ + QVERIFY(!(lhs == ineq)); \ + QVERIFY(ineq != rhs); \ + QVERIFY(!(ineq == rhs)); \ + /* end */ + +#define CHECK(lhs, rhs, ineq) \ + do { \ + CHECK_IMPL(lhs, rhs, ineq) \ + CHECK_IMPL(std::as_const(lhs), rhs, ineq) \ + CHECK_IMPL(lhs, std::as_const(rhs), ineq) \ + CHECK_IMPL(std::as_const(lhs), std::as_const(rhs), ineq) \ + } while (0) + + // check that the (in)equality operators aren't ambiguous in C++20: + QJsonArray a = {a0, a1}; + + static_assert(std::is_same_v); + + auto r0 = a.begin()[0]; + auto r1 = a.begin()[1]; + auto c0 = std::as_const(a).begin()[0]; + // ref <> ref + CHECK(r0, r0, r1); + // cref <> ref + CHECK(c0, r0, r1); + // ref <> cref + CHECK(r0, c0, r1); + // ref <> val + CHECK(r0, a0, r1); + // cref <> val + CHECK(c0, a0, r1); + // val <> ref + CHECK(a0, r0, a1); + // val <> cref + CHECK(a0, c0, a1); + // val <> val + CHECK(a0, a0, a1); + +#undef CHECK +#undef CHECK_IMPL +} + void tst_QtJson::testObjectIteration() { QJsonObject object;