path: root/src/corelib
diff options
authorMarc Mutz <>2016-05-10 10:49:11 +0200
committerMarc Mutz <>2016-05-11 16:55:22 +0000
commit81793b8b58cde01e52675093dc378147dc2102e3 (patch)
treeaf7819c28b7718327eb91d6466e9414d512af411 /src/corelib
parente64b2234e829cc47872225debcf80d6c06db18f0 (diff)
Add QT_NO_FOREACH to disable foreach and Q_FOREACH
It has been known for a long time that Q_FOREACH produces inferior code to other looping constructs, and the use of it in Qt library code was informally frowned upon since forever (pun intended). Yet, to this day, several thousand foreach/Q_FOREACH loops have been added to Qt libraries, and while many were ported to range-for in Qt 5.7, there are still new ones added every day, which is a nuisance, to say the least. This patch introduces a technical way to prevent new foreach use to creep into Qt libraries after they have been cleaned, by simply not defining either Q_FOREACH or foreach when the QT_NO_FOREACH macro is defined. This way, one library at a time can be ported away, and, once ported, is guaranteed to actually stay ported. Change-Id: Ie042e84d6c7d766bd16095f9bc1118a8e0ce0c7a Reviewed-by: Thiago Macieira <>
Diffstat (limited to 'src/corelib')
1 files changed, 6 insertions, 0 deletions
diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h
index a72bdb4d59..e35ee0987f 100644
--- a/src/corelib/global/qglobal.h
+++ b/src/corelib/global/qglobal.h
@@ -932,6 +932,8 @@ QT_WARNING_DISABLE_MSVC(4530) /* C++ exception handler used, but unwind semantic
# endif
+#ifndef QT_NO_FOREACH
template <typename T>
class QForeachContainer {
QForeachContainer &operator=(const QForeachContainer &) Q_DECL_EQ_DELETE;
@@ -957,11 +959,15 @@ for (QForeachContainer<typename QtPrivate::remove_reference<decltype(container)>
++_container_.i, _container_.control ^= 1) \
for (variable = *_container_.i; _container_.control; _container_.control = 0)
+#endif // QT_NO_FOREACH
#define Q_FOREVER for(;;)
+# ifndef QT_NO_FOREACH
# ifndef foreach
# define foreach Q_FOREACH
# endif
+# endif // QT_NO_FOREACH
# ifndef forever
# define forever Q_FOREVER
# endif