diff --git a/src/corelib/io/qdebug.cpp b/src/corelib/io/qdebug.cpp index 9b64be67ceb..e78191e1ec2 100644 --- a/src/corelib/io/qdebug.cpp +++ b/src/corelib/io/qdebug.cpp @@ -1259,10 +1259,9 @@ needs to support streaming into QDebug. /*! \since 6.7 - \fn template QDebug operator<<(QDebug debug, const std::optional &opt) - \relates QDebug + \fn template > QDebug::operator<<(const std::optional &opt) - Writes the contents of \a opt (or \c nullopt if not set) to \a debug. + Writes the contents of \a opt (or \c nullopt if not set) to this stream. \c T needs to support streaming into QDebug. */ diff --git a/src/corelib/io/qdebug.h b/src/corelib/io/qdebug.h index f12b7dba13d..737da819705 100644 --- a/src/corelib/io/qdebug.h +++ b/src/corelib/io/qdebug.h @@ -296,6 +296,16 @@ public: return putTupleLike("std", "tuple", t); } + template = true> + QDebug &operator<<(const std::optional &o) + { + if (!o) + return *this << std::nullopt; + StreamTypeErased s = &streamTypeErased>; + const void *d = std::addressof(*o); + return putTupleLikeImplImpl("std", "optional", 1, &s, &d); + } + private: template using if_ordering_type = std::enable_if_t, bool>; @@ -487,16 +497,6 @@ inline QDebugIfHasDebugStreamContainer, Key, T> operator<<(QD return QtPrivate::printAssociativeContainer(std::move(debug), "QMultiHash", hash); } -template -inline QDebugIfHasDebugStream operator<<(QDebug debug, const std::optional &opt) -{ - if (!opt) - return debug << std::nullopt; - const QDebugStateSaver saver(debug); - debug.nospace() << "std::optional(" << *opt << ')'; - return debug; -} - template inline QDebugIfHasDebugStream operator<<(QDebug debug, const std::pair &pair) {