X Tutup
Skip to content

ALL: Use C++ 11 range-based for loops#6508

Merged
sev- merged 58 commits intoscummvm:masterfrom
bluegr:c11forloops
Mar 27, 2025
Merged

ALL: Use C++ 11 range-based for loops#6508
sev- merged 58 commits intoscummvm:masterfrom
bluegr:c11forloops

Conversation

@bluegr
Copy link
Member

@bluegr bluegr commented Mar 24, 2025

This changes most places in common code where range-based for loops are used to C++ 11 range-based for loops.

Therefore, loops like these:

for (Common::Array<const ControlEvent*>::iterator i = _controlEvents.begin(); i != _controlEvents.end(); ++i)

have been changed to their C++ 11 equivalent:

for (auto &event : _controlEvents)

I've used the following regexp to find and replace most cases:

for \([\w<>:]*\w*[_]*iterator (\w*) = ([\w.]*).begin\(\); [\w.]* != [\w.]*.end\(\); [\+]*[\w.]*[\+]*\) {

and the replace macro:

for (auto &$1 : $2) {

I've then checked each change, and adapted the code inside the loops accordingly.

Only straightforward loops have been adapted, as mentioned above. The following loops have been left with the existing semantics:

  • loops where the iterator is modified internally
  • loops in functions that return an iterator
  • loops where erase() is called
  • loops where insert() is called
  • loops that store the current value of the iterator externally
  • loops with reverse iterators
  • specialized cases

Most of the common code has been adapted, and a lot of the engine code has been adapted, too. Since this is a lengthy process, the rest of the engines will be adapted in a future PR.

Thoughts and opinions are welcome :)

@sev-
Copy link
Member

sev- commented Mar 27, 2025

I am merging this before it gets bitrot. The rest could be continued in-tree.

Thank you, Filippos, for improving our codebase readability!

@sev- sev- merged commit 5b1e2d5 into scummvm:master Mar 27, 2025
8 checks passed
@bluegr bluegr deleted the c11forloops branch March 27, 2025 05:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

X Tutup