meven added a comment.
In D27052#603798 <https://phabricator.kde.org/D27052#603798>, @alex wrote: > Hello, > I have a question regarding your changed. > > you sometimes declare the variables you want to loop over before the loop, but sometimes in the loop. > For instance in DeviceModel.cpp line 135 it is declared as a const before the loop and in line 139 it is directly used in the loop. > > In one of my previous revisions I have implemented it like in line 139 and https://phabricator.kde.org/D26912?id=74352#inline-152211 and changes were requested. > > Am I missing conceptual differences between these use cases ? > > Thanks again for your expertise ! The important thing is as long as you don't need to change in a loop the container, your container should be const to be used in a for loop, so that semantically the for loop is equivalent to the foreach version, but avoiding the copying foreach did. So either your container is a variable, it needs to be const, if not use qAsConst, and if it is a function you don't need to do anything if the function is const (`keys()`), if not the best practice is to introduce an intermediate const variable. The long version about this Q_FOREACH / foreach is at https://www.kdab.com/goodbye-q_foreach/ REPOSITORY R119 Plasma Desktop REVISION DETAIL https://phabricator.kde.org/D27052 To: meven, broulik, ervin, #plasma Cc: alex, plasma-devel, Orage, LeGast00n, The-Feren-OS-Dev, jraleigh, zachus, fbampaloukas, GB_2, ragreen, ZrenBot, ngraham, alexeymin, himcesjf, lesliezhai, ali-mohamed, jensreuterberg, abetts, sebas, apol, ahiemstra, mart