From 9e18e5c869dbef1f0d9a79e899538ab680e14772 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 15 Mar 2023 09:54:29 +0100 Subject: [PATCH] Modernize Tuple Protocol implementations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The existing get() implementations are very cleverâ„¢, if I, as their inventor in at least the Qt ecosystem, may say so myself. E.g. the () around the return value are absolutely required, to ensure we return by reference, not by value, through decltype(auto). C++23 gives us forward_like, which doesn't require the parentheses, because it always returns a reference. Also replace decay_t with C++20's remove_cvref_t, the latter being more efficient (performs less work). Change-Id: Ic9ed0c25e6d6bfbd77d7c85858a8d97fe58be615 Reviewed-by: Thiago Macieira Reviewed-by: Qt CI Bot --- src/corelib/tools/qmargins.h | 23 +++++++++++++---------- src/corelib/tools/qpoint.h | 15 +++++++++------ src/corelib/tools/qsize.h | 15 +++++++++------ src/gui/math3d/qvectornd.h | 15 +++++++++------ 4 files changed, 40 insertions(+), 28 deletions(-) diff --git a/src/corelib/tools/qmargins.h b/src/corelib/tools/qmargins.h index 2d494f4417..0b4f74e9dd 100644 --- a/src/corelib/tools/qmargins.h +++ b/src/corelib/tools/qmargins.h @@ -6,6 +6,9 @@ #include +#include +#include + QT_BEGIN_NAMESPACE class QMarginsF; @@ -66,17 +69,17 @@ private: template = true, - std::enable_if_t, QMargins>, bool> = true> + std::enable_if_t, QMargins>, bool> = true> friend constexpr decltype(auto) get(M &&m) noexcept { if constexpr (I == 0) - return (std::forward(m).m_left); + return q23::forward_like(m.m_left); else if constexpr (I == 1) - return (std::forward(m).m_top); + return q23::forward_like(m.m_top); else if constexpr (I == 2) - return (std::forward(m).m_right); + return q23::forward_like(m.m_right); else if constexpr (I == 3) - return (std::forward(m).m_bottom); + return q23::forward_like(m.m_bottom); } }; @@ -315,17 +318,17 @@ private: template = true, - std::enable_if_t, QMarginsF>, bool> = true> + std::enable_if_t, QMarginsF>, bool> = true> friend constexpr decltype(auto) get(M &&m) noexcept { if constexpr (I == 0) - return (std::forward(m).m_left); + return q23::forward_like(m.m_left); else if constexpr (I == 1) - return (std::forward(m).m_top); + return q23::forward_like(m.m_top); else if constexpr (I == 2) - return (std::forward(m).m_right); + return q23::forward_like(m.m_right); else if constexpr (I == 3) - return (std::forward(m).m_bottom); + return q23::forward_like(m.m_bottom); } }; diff --git a/src/corelib/tools/qpoint.h b/src/corelib/tools/qpoint.h index 48c5e91f62..425948966d 100644 --- a/src/corelib/tools/qpoint.h +++ b/src/corelib/tools/qpoint.h @@ -6,6 +6,9 @@ #include +#include +#include + #if defined(Q_OS_DARWIN) || defined(Q_QDOC) struct CGPoint; #endif @@ -86,13 +89,13 @@ private: template = true, - std::enable_if_t, QPoint>, bool> = true> + std::enable_if_t, QPoint>, bool> = true> friend constexpr decltype(auto) get(P &&p) noexcept { if constexpr (I == 0) - return (std::forward

(p).xp); + return q23::forward_like

(p.xp); else if constexpr (I == 1) - return (std::forward

(p).yp); + return q23::forward_like

(p.yp); } }; @@ -283,13 +286,13 @@ private: template = true, - std::enable_if_t, QPointF>, bool> = true> + std::enable_if_t, QPointF>, bool> = true> friend constexpr decltype(auto) get(P &&p) noexcept { if constexpr (I == 0) - return (std::forward

(p).xp); + return q23::forward_like

(p.xp); else if constexpr (I == 1) - return (std::forward

(p).yp); + return q23::forward_like

(p.yp); } }; diff --git a/src/corelib/tools/qsize.h b/src/corelib/tools/qsize.h index 71066c35c0..dac2a48908 100644 --- a/src/corelib/tools/qsize.h +++ b/src/corelib/tools/qsize.h @@ -8,6 +8,9 @@ #include #include +#include +#include + #if defined(Q_OS_DARWIN) || defined(Q_QDOC) struct CGSize; #endif @@ -83,13 +86,13 @@ private: template = true, - std::enable_if_t, QSize>, bool> = true> + std::enable_if_t, QSize>, bool> = true> friend constexpr decltype(auto) get(S &&s) noexcept { if constexpr (I == 0) - return (std::forward(s).wd); + return q23::forward_like(s.wd); else if constexpr (I == 1) - return (std::forward(s).ht); + return q23::forward_like(s.ht); } }; Q_DECLARE_TYPEINFO(QSize, Q_RELOCATABLE_TYPE); @@ -272,13 +275,13 @@ private: template = true, - std::enable_if_t, QSizeF>, bool> = true> + std::enable_if_t, QSizeF>, bool> = true> friend constexpr decltype(auto) get(S &&s) noexcept { if constexpr (I == 0) - return (std::forward(s).wd); + return q23::forward_like(s.wd); else if constexpr (I == 1) - return (std::forward(s).ht); + return q23::forward_like(s.ht); } }; Q_DECLARE_TYPEINFO(QSizeF, Q_RELOCATABLE_TYPE); diff --git a/src/gui/math3d/qvectornd.h b/src/gui/math3d/qvectornd.h index 0f3ce758a8..639b8f0e5a 100644 --- a/src/gui/math3d/qvectornd.h +++ b/src/gui/math3d/qvectornd.h @@ -10,6 +10,9 @@ #include #include +#include +#include + QT_BEGIN_NAMESPACE class QVector2D; @@ -145,10 +148,10 @@ private: template = true, - std::enable_if_t, QVector2D>, bool> = true> + std::enable_if_t, QVector2D>, bool> = true> friend constexpr decltype(auto) get(V &&vec) noexcept { - return (std::forward(vec).v[I]); + return q23::forward_like(vec.v[I]); } }; @@ -304,10 +307,10 @@ private: template = true, - std::enable_if_t, QVector3D>, bool> = true> + std::enable_if_t, QVector3D>, bool> = true> friend constexpr decltype(auto) get(V &&vec) noexcept { - return (std::forward(vec).v[I]); + return q23::forward_like(vec.v[I]); } }; @@ -456,10 +459,10 @@ private: template = true, - std::enable_if_t, QVector4D>, bool> = true> + std::enable_if_t, QVector4D>, bool> = true> friend constexpr decltype(auto) get(V &&vec) noexcept { - return (std::forward(vec).v[I]); + return q23::forward_like(vec.v[I]); } };