diff --git a/src/corelib/global/qglobal.cpp b/src/corelib/global/qglobal.cpp index 88a61562cb..3436a19881 100644 --- a/src/corelib/global/qglobal.cpp +++ b/src/corelib/global/qglobal.cpp @@ -3805,6 +3805,17 @@ bool qunsetenv(const char *varName) \snippet code/src_corelib_global_qglobal.cpp as-const-4 */ +/*! + \fn template T qExchange(T &obj, U &&newValue) + \relates + \since 5.14 + + Replaces the value of \a obj with \a newValue and returns the old value of \a obj. + + This is Qt's implementation of std::exchange(). It differs from std::exchange() + only in that it is \c constexpr already in C++14, and available on all supported + compilers. +*/ /*! \macro QT_TR_NOOP(sourceText) \relates diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index 0a0c434a07..24d250d923 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -1008,6 +1008,15 @@ Q_DECL_CONSTEXPR typename std::add_const::type &qAsConst(T &t) noexcept { ret template void qAsConst(const T &&) = delete; +// like std::exchange +template +Q_DECL_RELAXED_CONSTEXPR T qExchange(T &t, U &&newValue) +{ + T old = std::move(t); + t = std::forward(newValue); + return old; +} + #ifndef QT_NO_FOREACH namespace QtPrivate {