From e1b76ee928beff434d220c7711ea6798318d367e Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 5 Dec 2022 11:12:55 +0100 Subject: [PATCH] Remove the last in-tree user of qExchange() and mark all of Qt free of it We've ported all qExchange() to std::exchange by now, across all modules, but the one in QScopedValueRollback was left behind, because it requires C++20's version of std::exchange (constexpr). Since q20::exchange was not approved, replace the qExchange() here with two moves and add a comment to port to std::exchange() once we can depend on C++20. Then add QT_NO_QEXCHANGE to avoid new uses from creeping in. Change-Id: I488e252433e78fb2766639dbe77a22a55196cfd1 Reviewed-by: Volker Hilsheimer --- cmake/QtInternalTargets.cmake | 1 + src/corelib/global/qttypetraits.h | 4 ++++ src/corelib/tools/qscopedvaluerollback.h | 3 ++- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/cmake/QtInternalTargets.cmake b/cmake/QtInternalTargets.cmake index dc45b57eec..8de5f8a080 100644 --- a/cmake/QtInternalTargets.cmake +++ b/cmake/QtInternalTargets.cmake @@ -151,6 +151,7 @@ qt_internal_add_target_aliases(PlatformToolInternal) target_link_libraries(PlatformToolInternal INTERFACE PlatformAppInternal) qt_internal_add_global_definition(QT_NO_JAVA_STYLE_ITERATORS) +qt_internal_add_global_definition(QT_NO_QEXCHANGE) qt_internal_add_global_definition(QT_NO_NARROWING_CONVERSIONS_IN_CONNECT) qt_internal_add_global_definition(QT_EXPLICIT_QFILE_CONSTRUCTION_FROM_PATH) diff --git a/src/corelib/global/qttypetraits.h b/src/corelib/global/qttypetraits.h index 7e991ab8ca..a7bb4e5353 100644 --- a/src/corelib/global/qttypetraits.h +++ b/src/corelib/global/qttypetraits.h @@ -34,6 +34,8 @@ void qAsConst(const T &&) = delete; #endif // QT_NO_AS_CONST +#ifndef QT_NO_QEXCHANGE + // like std::exchange template constexpr T qExchange(T &t, U &&newValue) @@ -45,6 +47,8 @@ noexcept(std::conjunction_v, return old; } +#endif // QT_NO_QEXCHANGE + namespace QtPrivate { // helper to be used to trigger a "dependent static_assert(false)" // (for instance, in a final `else` branch of a `if constexpr`.) diff --git a/src/corelib/tools/qscopedvaluerollback.h b/src/corelib/tools/qscopedvaluerollback.h index a4a451b0c7..53a31e4675 100644 --- a/src/corelib/tools/qscopedvaluerollback.h +++ b/src/corelib/tools/qscopedvaluerollback.h @@ -18,8 +18,9 @@ public: } explicit constexpr QScopedValueRollback(T &var, T value) - : varRef(var), oldValue(qExchange(var, std::move(value))) + : varRef(var), oldValue(std::move(var)) // ### C++20: std::exchange(var, std::move(value)) { + var = std::move(value); } #if __cpp_constexpr >= 201907L