| 1 | // Copyright (C) 2023 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | #ifndef Q23UTILITY_H |
| 4 | #define Q23UTILITY_H |
| 5 | |
| 6 | #include <QtCore/qtconfigmacros.h> |
| 7 | |
| 8 | #include <QtCore/q20utility.h> |
| 9 | |
| 10 | // |
| 11 | // W A R N I N G |
| 12 | // ------------- |
| 13 | // |
| 14 | // This file is not part of the Qt API. Types and functions defined in this |
| 15 | // file can reliably be replaced by their std counterparts, once available. |
| 16 | // You may use these definitions in your own code, but be aware that we |
| 17 | // will remove them once Qt depends on the C++ version that supports |
| 18 | // them in namespace std. There will be NO deprecation warning, the |
| 19 | // definitions will JUST go away. |
| 20 | // |
| 21 | // If you can't agree to these terms, don't use these definitions! |
| 22 | // |
| 23 | // We mean it. |
| 24 | // |
| 25 | |
| 26 | QT_BEGIN_NAMESPACE |
| 27 | |
| 28 | namespace q23 { |
| 29 | // like std::forward_like |
| 30 | #ifdef __cpp_lib_forward_like |
| 31 | using std::forward_like; |
| 32 | #else |
| 33 | |
| 34 | namespace _detail { |
| 35 | |
| 36 | // [forward]/6.1 COPY_CONST |
| 37 | template <typename A, typename B> |
| 38 | using copy_const_t = std::conditional_t< |
| 39 | std::is_const_v<A>, const B, |
| 40 | /* else */ B |
| 41 | >; |
| 42 | |
| 43 | // [forward]/6.2 OVERRIDE_REF |
| 44 | template <typename A, typename B> |
| 45 | using override_ref_t = std::conditional_t< |
| 46 | std::is_rvalue_reference_v<A>, std::remove_reference_t<B>&&, |
| 47 | /* else */ B& |
| 48 | >; |
| 49 | |
| 50 | // [forward]/6.3 "V" |
| 51 | template <typename T, typename U> |
| 52 | using forward_like_ret_t = override_ref_t< |
| 53 | T&&, |
| 54 | copy_const_t< |
| 55 | std::remove_reference_t<T>, |
| 56 | std::remove_reference_t<U> |
| 57 | > |
| 58 | >; |
| 59 | |
| 60 | } // namespace detail |
| 61 | |
| 62 | // http://eel.is/c++draft/forward#lib:forward_like |
| 63 | template <class T, class U> |
| 64 | [[nodiscard]] constexpr auto forward_like(U &&x) noexcept |
| 65 | -> _detail::forward_like_ret_t<T, U> |
| 66 | { |
| 67 | using V = _detail::forward_like_ret_t<T, U>; |
| 68 | return static_cast<V>(x); |
| 69 | } |
| 70 | #endif // __cpp_lib_forward_like |
| 71 | } // namespace q23 |
| 72 | |
| 73 | QT_END_NAMESPACE |
| 74 | |
| 75 | #endif /* Q23UTILITY_H */ |
| 76 | |