diff --git a/src/corelib/io/qdebug.cpp b/src/corelib/io/qdebug.cpp index 70832c3dbcb..600b8b13b80 100644 --- a/src/corelib/io/qdebug.cpp +++ b/src/corelib/io/qdebug.cpp @@ -1002,6 +1002,17 @@ QDebug &QDebug::resetFormat() \include qdebug-toString.qdocinc */ +/*! \internal */ +QString QDebug::toStringImpl(StreamTypeErased s, const void *obj) +{ + QString result; + { + QDebug d(&result); + s(d.nospace(), obj); + } + return result; +} + /*! \fn template QDebug operator<<(QDebug debug, const QList &list) \relates QDebug diff --git a/src/corelib/io/qdebug.h b/src/corelib/io/qdebug.h index 35ce50a0f15..e554e92c60f 100644 --- a/src/corelib/io/qdebug.h +++ b/src/corelib/io/qdebug.h @@ -223,13 +223,19 @@ public: QDebug &operator<<(T u128) { putUInt128(&u128); return maybeSpace(); } #endif // QT_SUPPORTS_INT128 +private: + template + static void streamTypeErased(QDebug &d, const void *obj) + { + d << *static_cast(obj); + } + using StreamTypeErased = void(*)(QDebug&, const void*); + QT7_ONLY(Q_CORE_EXPORT) static QString toStringImpl(StreamTypeErased s, const void *obj); +public: template static QString toString(const T &object) { - QString buffer; - QDebug stream(&buffer); - stream.nospace() << object; - return buffer; + return toStringImpl(&streamTypeErased, &object); } };