From 28f56f29bb9b9874c5df41609ef4979d120333a1 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 3 Mar 2023 07:04:21 +0100 Subject: [PATCH] Short live q23::forward_like! Following https://eel.is/c++draft/forward#lib:forward_like step-by-step. Temporarily add a q23utility.cpp to contain the static_assert() tests. There's no point in establishing a tst_q23utility just to run six static assertions. Should remove the .cpp again once an in-tree user exercises the function with the same coverage. Task-number: QTBUG-111598 Change-Id: Icb18eae089d65ca4e555921dbb71e74a7e255645 Reviewed-by: Thiago Macieira --- src/corelib/CMakeLists.txt | 2 + src/corelib/global/q23utility.cpp | 25 +++++++++++ src/corelib/global/q23utility.h | 75 +++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 src/corelib/global/q23utility.cpp create mode 100644 src/corelib/global/q23utility.h diff --git a/src/corelib/CMakeLists.txt b/src/corelib/CMakeLists.txt index 1e611fa7e9..09ed1f9e77 100644 --- a/src/corelib/CMakeLists.txt +++ b/src/corelib/CMakeLists.txt @@ -104,6 +104,8 @@ qt_internal_add_module(Core global/q20memory.h global/q20type_traits.h global/q23functional.h + global/q23utility.cpp # remove once we have a user that tests this + global/q23utility.h global/qxpfunctional.h global/qxptype_traits.h ipc/qsharedmemory.cpp ipc/qsharedmemory.h ipc/qsharedmemory_p.h diff --git a/src/corelib/global/q23utility.cpp b/src/corelib/global/q23utility.cpp new file mode 100644 index 0000000000..9c5365c547 --- /dev/null +++ b/src/corelib/global/q23utility.cpp @@ -0,0 +1,25 @@ +// Copyright (C) 2023 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 + +#include + +QT_BEGIN_NAMESPACE + +#define CHECK2(cvref_in, cvref_out) \ + static_assert(std::is_same_v< \ + decltype(q23::forward_like(std::declval())), \ + long cvref_out \ + >, "oops: cvref '" #cvref_in "' doesn't work") \ + /* end */ +#define CHECK(cvref) CHECK2(cvref, cvref) +CHECK2(/**/, &&); +CHECK(&); +CHECK(&&); +CHECK2(const, const &&); +CHECK(const &); +CHECK(const &&); +// volatile is not supported +#undef CHECK +#undef CHECK2 + +QT_END_NAMESPACE diff --git a/src/corelib/global/q23utility.h b/src/corelib/global/q23utility.h new file mode 100644 index 0000000000..9ae5389b56 --- /dev/null +++ b/src/corelib/global/q23utility.h @@ -0,0 +1,75 @@ +// Copyright (C) 2023 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 Q23UTILITY_H +#define Q23UTILITY_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 + +namespace q23 { +// like std::forward_like +#ifdef __cpp_lib_forward_like +using std::forward_like; +#else + +namespace _detail { + +// [forward]/6.1 COPY_CONST +template +using copy_const_t = std::conditional_t< + std::is_const_v, const B, + /* else */ B + >; + +// [forward]/6.2 OVERRIDE_REF +template +using override_ref_t = std::conditional_t< + std::is_rvalue_reference_v, std::remove_reference_t&&, + /* else */ B& + >; + +// [forward]/6.3 "V" +template +using forward_like_ret_t = override_ref_t< + T&&, + copy_const_t< + std::remove_reference_t, + std::remove_reference_t + > + >; + +} // namespace detail + +// http://eel.is/c++draft/forward#lib:forward_like +template +[[nodiscard]] constexpr auto forward_like(U &&x) noexcept + -> _detail::forward_like_ret_t +{ + using V = _detail::forward_like_ret_t; + return static_cast(x); +} +#endif // __cpp_lib_forward_like +} // namespace q23 + +QT_END_NAMESPACE + +#endif /* Q23UTILITY_H */