Short live qExchange()!

This is a 1:1 replacement for std::exchange, and should be removed once
Qt fully depends on C++14. It's too versatile a tool to miss it, so
provide a copy.

[ChangeLog][QtCore][QtGlobal] Added qExchange(), a drop-in for C++14's std::exchange()

Change-Id: I31c4f1141e7a99f99ea65eb36ddf9d68b7847337
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Marc Mutz 2019-05-13 20:43:27 +02:00
parent 969e60e075
commit a73385e2cf
2 changed files with 20 additions and 0 deletions

View File

@ -3805,6 +3805,17 @@ bool qunsetenv(const char *varName)
\snippet code/src_corelib_global_qglobal.cpp as-const-4
*/
/*!
\fn template <typename T, typename U = T> T qExchange(T &obj, U &&newValue)
\relates <QtGlobal>
\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 <QtGlobal>

View File

@ -1008,6 +1008,15 @@ Q_DECL_CONSTEXPR typename std::add_const<T>::type &qAsConst(T &t) noexcept { ret
template <typename T>
void qAsConst(const T &&) = delete;
// like std::exchange
template <typename T, typename U = T>
Q_DECL_RELAXED_CONSTEXPR T qExchange(T &t, U &&newValue)
{
T old = std::move(t);
t = std::forward<U>(newValue);
return old;
}
#ifndef QT_NO_FOREACH
namespace QtPrivate {