Short live q20::exchange()!
We have weaned ourselves off of qExchange(), which, however, has C++23 semantics (is constexpr and noexcept), while we only require C++17 atm. At the same time, we have more and more uses of a constexpr exchange(), iow: the C++20 extension. We have the qNN namespaces for this, so let's use it: Add q20::exchange(), with C++20 semantics (constexpr, not noexcept), and use it to port the only remaining qExchange() user (since ported to 2×std::move() to make the header compatible with QT_NO_QEXCHANGE), QScopedValueRollback. No user requires the C++23 noexcept on the function (compilers will figure it out, because the std::exchange is fully inline), and we can't provide a q23::exchange() even if we needed to, because the author of P2401 forgot to ask to update the value of __cpp_lib_exchange_function... Fixes: QTBUG-133038 Pick-to: 6.8 Change-Id: I003df445001d1c5ab02402119c5e5106aa156263 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> (cherry picked from commit b5ed7fb203335cef7b7b4f70f4130b07166cecf2) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
c88a8c78bd
commit
cebb2dd068
@ -118,6 +118,21 @@ constexpr bool in_range(T t) noexcept
|
|||||||
#endif // __cpp_lib_integer_comparison_functions
|
#endif // __cpp_lib_integer_comparison_functions
|
||||||
} // namespace q20
|
} // namespace q20
|
||||||
|
|
||||||
|
// like C++20 std::exchange (ie. constexpr, not yet noexcept)
|
||||||
|
namespace q20 {
|
||||||
|
#ifdef __cpp_lib_constexpr_algorithms
|
||||||
|
using std::exchange;
|
||||||
|
#else
|
||||||
|
template <typename T, typename U = T>
|
||||||
|
constexpr T exchange(T& obj, U&& newValue)
|
||||||
|
{
|
||||||
|
T old = std::move(obj);
|
||||||
|
obj = std::forward<U>(newValue);
|
||||||
|
return old;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
QT_END_NAMESPACE
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
#endif /* Q20UTILITY_H */
|
#endif /* Q20UTILITY_H */
|
||||||
|
@ -6,6 +6,8 @@
|
|||||||
|
|
||||||
#include <QtCore/qglobal.h>
|
#include <QtCore/qglobal.h>
|
||||||
|
|
||||||
|
#include <QtCore/q20utility.h>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@ -20,9 +22,8 @@ public:
|
|||||||
|
|
||||||
Q_NODISCARD_CTOR
|
Q_NODISCARD_CTOR
|
||||||
explicit constexpr QScopedValueRollback(T &var, T value)
|
explicit constexpr QScopedValueRollback(T &var, T value)
|
||||||
: varRef(var), oldValue(std::move(var)) // ### C++20: std::exchange(var, std::move(value))
|
: varRef(var), oldValue(q20::exchange(var, std::move(value)))
|
||||||
{
|
{
|
||||||
var = std::move(value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Q_DECL_CONSTEXPR_DTOR
|
Q_DECL_CONSTEXPR_DTOR
|
||||||
|
Loading…
x
Reference in New Issue
Block a user