From 0ed63f0a2bfe875a09f8c3ba18eafd2b4e15e8da Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 11 Feb 2014 14:58:46 -0800 Subject: [PATCH] Add the option of using decltype() in Q_FOREACH This should allow more compilers to use the shorter & simpler version of the macro. Change-Id: Ibd2bf20f479b707a7806d0265a5580ebfc2e7733 Reviewed-by: Olivier Goffart Reviewed-by: Marc Mutz --- src/corelib/global/qglobal.h | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index dccf5590f0..59d72396cf 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -858,8 +858,8 @@ Q_CORE_EXPORT void qFreeAligned(void *ptr); # endif #endif -#if (defined(Q_CC_GNU) && !defined(Q_CC_RVCT)) -/* make use of typeof-extension */ +#if defined(Q_COMPILER_DECLTYPE) || (defined(Q_CC_GNU) && !defined(Q_CC_RVCT)) +/* make use of decltype or GCC's __typeof__ extension */ template class QForeachContainer { public: @@ -869,6 +869,11 @@ public: int control; }; +# ifdef Q_COMPILER_DECLTYPE +# define QT_FOREACH_DECLTYPE(x) typename QtPrivate::remove_reference::type +# else +# define QT_FOREACH_DECLTYPE(x) __typeof__((x)) +# endif // Explanation of the control word: // - it's initialized to 1 @@ -880,7 +885,7 @@ public: // - if there was a break inside the inner loop, it will exit with control still // set to 1; in that case, the outer loop will invert it to 0 and will exit too # define Q_FOREACH(variable, container) \ -for (QForeachContainer<__typeof__((container))> _container_((container)); \ +for (QForeachContainer _container_((container)); \ _container_.control && _container_.i != _container_.e; \ ++_container_.i, _container_.control ^= 1) \ for (variable = *_container_.i; _container_.control; _container_.control = 0)