Call std::addressof instead of operator& in QDebug::toString

QDebug::toString intends to pass the input object's address to its
nested implementation, but was calling operator& which does not work
with types that has a custom overload. Calling std::addressof fixes this
problem.

Fixes: QTBUG-127510
Change-Id: Ie608f7b1a63c4032246b6ff98a3651695f0536ca
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
(cherry picked from commit 6cd6c3d6d70f8e76059153dd58ed2c61af2889b3)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Jøger Hansegård 2024-07-24 18:54:21 +02:00 committed by Qt Cherry-pick Bot
parent 633013827a
commit b64e918c85
2 changed files with 15 additions and 1 deletions

View File

@ -21,6 +21,7 @@
#include <chrono>
#include <list>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
@ -235,7 +236,7 @@ public:
template <typename T>
static QString toString(const T &object)
{
return toStringImpl(&streamTypeErased<T>, &object);
return toStringImpl(&streamTypeErased<T>, std::addressof(object));
}
};

View File

@ -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