diff --git a/src/corelib/CMakeLists.txt b/src/corelib/CMakeLists.txt index 1b771e67a6..faf9a349f1 100644 --- a/src/corelib/CMakeLists.txt +++ b/src/corelib/CMakeLists.txt @@ -96,6 +96,7 @@ qt_internal_add_module(Core global/q20map.h global/q20memory.h global/q20type_traits.h + global/q20utility.h global/q20vector.h global/q23functional.h global/q23utility.cpp # remove once we have a user that tests this diff --git a/src/corelib/global/q20utility.h b/src/corelib/global/q20utility.h new file mode 100644 index 0000000000..9d33500c93 --- /dev/null +++ b/src/corelib/global/q20utility.h @@ -0,0 +1,46 @@ +// Copyright (C) 2024 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#ifndef Q20UTILITY_H +#define Q20UTILITY_H + +#include + +#include + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. Types and functions defined in this +// file can reliably be replaced by their std counterparts, once available. +// You may use these definitions in your own code, but be aware that we +// will remove them once Qt depends on the C++ version that supports +// them in namespace std. There will be NO deprecation warning, the +// definitions will JUST go away. +// +// If you can't agree to these terms, don't use these definitions! +// +// We mean it. +// + +QT_BEGIN_NAMESPACE + +// 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/global/q23utility.h b/src/corelib/global/q23utility.h index 9ae5389b56..3ac5d23e32 100644 --- a/src/corelib/global/q23utility.h +++ b/src/corelib/global/q23utility.h @@ -5,7 +5,7 @@ #include -#include +#include // // W A R N I N G diff --git a/src/corelib/tools/qscopedvaluerollback.h b/src/corelib/tools/qscopedvaluerollback.h index 0ae3efd0c0..d79366bcc5 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); } #if __cpp_constexpr >= 201907L