From 4042596b72520878c6d418bac8f6fdd121c33520 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 23 May 2022 16:43:03 +0200 Subject: [PATCH] Short live q23::invoke_r! Add an implementation of C++23's std::invoke_r. Will need it in our implementation of function_ref. Differences to std::invoke_r: - only constexpr in C++20, not in C++17 (to fix, we'd need to implement our own q20::invoke just to add the constexpr keyword; let's first see whether we need this). Task-number: QTBUG-103739 Change-Id: Id864f83f27ed499849b6bde0c34353127e37e7ed Reviewed-by: Thiago Macieira Reviewed-by: Andrei Golubev --- src/corelib/CMakeLists.txt | 1 + src/corelib/global/q23functional.h | 49 ++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 src/corelib/global/q23functional.h diff --git a/src/corelib/CMakeLists.txt b/src/corelib/CMakeLists.txt index e9ac7b9428..a802e1140d 100644 --- a/src/corelib/CMakeLists.txt +++ b/src/corelib/CMakeLists.txt @@ -72,6 +72,7 @@ qt_internal_add_module(Core global/qvolatile_p.h global/q20algorithm.h global/q20functional.h + global/q23functional.h global/q20iterator.h io/qabstractfileengine.cpp io/qabstractfileengine_p.h io/qbuffer.cpp io/qbuffer.h diff --git a/src/corelib/global/q23functional.h b/src/corelib/global/q23functional.h new file mode 100644 index 0000000000..5f83df698e --- /dev/null +++ b/src/corelib/global/q23functional.h @@ -0,0 +1,49 @@ +// Copyright (C) 2022 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 Q23FUNCTIONAL_H +#define Q23FUNCTIONAL_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 will behave exactly as their std counterparts. 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 + +namespace q23 { +// like std::invoke_r +#ifdef __cpp_lib_invoke_r +using std::invoke_r; +#else +template +constexpr +std::enable_if_t, R> +invoke_r(F&& f, Args&&... args) + noexcept(std::is_nothrow_invocable_r_v) +{ + // ### use q20::invoke for a constexpr std::invoke + if constexpr (std::is_void_v) + std::invoke(std::forward(f), std::forward(args)...); + else + return std::invoke(std::forward(f), std::forward(args)...); +} +#endif // __cpp_lib_invoke_r +} // namespace q23 + +QT_END_NAMESPACE + +#endif /* Q23FUNCTIONAL_H */