In C++20 the new namespace std::ranges
was added to the standard, with many algorithms there duplicating already-existing ones, e.g. std::find_if
and std::ranges::find_if
. On the cppreference page the "old" algorithms are called "functions", and the "new" - "function-like entities". Yet, I can't use it in a "functional" way, e.g.
auto result = myMapObj
| std::ranges::views::keys
| std::ranges::find_if([](const auto& key) { return key == 42; });
I need to use it the same way as the good old std::find_if
. So what is the difference, and which one should I choose in which case?
They're global objects with overloaded
operator()
.There are some marginal benefits in designing them this way (compared to function templates), e.g. being able to pass them as arguments to other functions (
foo(std::ranges::find_if)
) using a sane syntax (to passstd::find_if
in this manner you'd need to wrap it in a lambda).You should always prefer the
std::ranges::...
versions, assuming your standard library implementation supports them.