Migrate to constrained algorithms
C++20 (which we use) introduces a new constrained algorithms feature, which allows to make a code which uses std's algorithms more readable. Usage examples:
- std::sort:
// before
std::sort(mSelection.begin(), mSelection.end(), Misc::StringUtils::CiComp{});
// after
std::ranges::sort(mSelection, Misc::StringUtils::CiComp{});
- std::find_if
// before
auto it
= std::find_if(creatureStats.mActiveSpells.mSpells.begin(), creatureStats.mActiveSpells.mSpells.end(),
[&](const auto& params) { return params.mId == spell.first; });
// after
auto it = std::ranges::find_if(creatureStats.mActiveSpells.mSpells,
[&](const auto& params) { return params.mId == spell.first; });
- std::for_each
// before
std::for_each(mGroups.begin(), mGroups.end(), [&](const auto& v) { mRootNode->addChild(v.second.mValue); });
// after
std::ranges::for_each(mGroups, [&](const auto& v) { mRootNode->addChild(v.second.mValue); });
Note that it also adds a LINQ-like algorithm composition, but it seems to be incomplete in C++20:
// before - reverse an original vector and iterate over it
std::reverse(candidates.begin(), candidates.end());
for (const auto& [pos, c] : candidates)
{}
// after - reverse a lightweight view for this vector and iterate over this view
for (const auto& [pos, c] : candidates | std::views::reverse)
{}
Edited by Andrei Kortunov