Skip to content

Instantly share code, notes, and snippets.

@jniemann66
Last active March 6, 2019 21:48
Show Gist options
  • Save jniemann66/d0bedb52f1117264952ce127267c8c21 to your computer and use it in GitHub Desktop.
Save jniemann66/d0bedb52f1117264952ce127267c8c21 to your computer and use it in GitHub Desktop.
Qt foreach vs ranged for

https://www.kdab.com/goodbye-q_foreach/

Conclusion

Here’s why you’ll want to port away from Q_FOREACH, ideally to C++11 ranged for-loops:

Q_FOREACH is deprectaed since 5.7
It only works efficiently on (some) Qt containers; it performs prohibitively expensive on all std containers, QVarLengthArray, and doesn’t work at all for C arrays.
Even where it works as advertised, it typically costs ~100 bytes of text size more per loop than the C++11 ranged for-loop.
Its unconditionally taking a copy of the container makes it hard to reason about the loop.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment