Bug 170270 - Use C++20 ranges copy_if, find_if or remove_if to simplify loops
Summary: Use C++20 ranges copy_if, find_if or remove_if to simplify loops
Status: NEW
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: LibreOffice (show other bugs)
Version:
(earliest affected)
unspecified
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords: difficultyBeginner, easyHack, skillCpp, topicCleanup
Depends on:
Blocks: Dev-related
  Show dependency treegraph
 
Reported: 2026-01-08 14:58 UTC by Hossein
Modified: 2026-01-10 20:43 UTC (History)
1 user (show)

See Also:
Crash report or crash signature:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Hossein 2026-01-08 14:58:27 UTC
Since C++20, it is possible to process containers in an easier form with std::ranges. One can rewrite loops with std::ranges functions. It is also simpler compared to C++11 syntax.

In tdf#170269, I have suggested using std::ranges::all_of, std::ranges::any_of and std::ranges::none_of. If you want to check/validate a condition over a range, refer to that issue. 

There are multiple functions available in ranges library:

Constrained algorithms (since C++20)
https://en.cppreference.com/w/cpp/algorithm/ranges.html

These are defined in <algorithm> header:

Copy certain elements of a range
std::ranges::copy_if
https://en.cppreference.com/w/cpp/algorithm/ranges/copy.html

Remove elements from a range
std::ranges::remove_if
https://en.cppreference.com/w/cpp/algorithm/ranges/remove.html

Find element in a range
std::ranges::find_if
https://en.cppreference.com/w/cpp/algorithm/ranges/find.html

You can find further information in the above links. These functions (except find_if) are part of "Modifying sequence operations".

The new std::ranges has a simpler syntax compared to the older std::copy_if, etc. functions used in tdf#153294, and is a replacement, in most cases.

Please make sure that you keep the code behavior unchanged.
Comment 1 Roman Kuznetsov 2026-01-10 20:43:19 UTC
Set to NEW as EasyHack