diff --git a/src/corelib/global/q20utility.h b/src/corelib/global/q20utility.h index b24c81b924d..96d89db7341 100644 --- a/src/corelib/global/q20utility.h +++ b/src/corelib/global/q20utility.h @@ -118,6 +118,21 @@ constexpr bool in_range(T t) noexcept #endif // __cpp_lib_integer_comparison_functions } // namespace q20 +// like C++20 std::exchange (ie. constexpr, not yet noexcept) +namespace q20 { +#ifdef __cpp_lib_constexpr_algorithms +using std::exchange; +#else +template +constexpr T exchange(T& obj, U&& newValue) +{ + T old = std::move(obj); + obj = std::forward(newValue); + return old; +} +#endif +} + QT_END_NAMESPACE #endif /* Q20UTILITY_H */ diff --git a/src/corelib/tools/qscopedvaluerollback.h b/src/corelib/tools/qscopedvaluerollback.h index bb24bfe2d1c..99da9e2cb22 100644 --- a/src/corelib/tools/qscopedvaluerollback.h +++ b/src/corelib/tools/qscopedvaluerollback.h @@ -6,6 +6,8 @@ #include +#include + QT_BEGIN_NAMESPACE template @@ -20,9 +22,8 @@ public: Q_NODISCARD_CTOR 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