diff --git a/src/corelib/io/qdebug.h b/src/corelib/io/qdebug.h index e554e92c60f..a2c270f5cd0 100644 --- a/src/corelib/io/qdebug.h +++ b/src/corelib/io/qdebug.h @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -235,7 +236,7 @@ public: template static QString toString(const T &object) { - return toStringImpl(&streamTypeErased, &object); + return toStringImpl(&streamTypeErased, std::addressof(object)); } }; diff --git a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp index 15da0758d0a..7ee2ff42cc6 100644 --- a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp +++ b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp @@ -1343,6 +1343,19 @@ void tst_QDebug::toString() const stream.nospace() << &qobject; QCOMPARE(QDebug::toString(&qobject), expectedString); } + + // Overloaded operator& + { + struct TypeWithAddressOf + { + int* operator&() const { return nullptr; } + operator QByteArray() const { return "test"; } + }; + + TypeWithAddressOf object; + QString expectedString {"\"test\""}; + QCOMPARE(QDebug::toString(object), expectedString); + } } void tst_QDebug::noQVariantEndlessRecursion() const