Why is there still no range-enabled reduction algorithm in std?
Asked Answered
A

1

21

The only options available are std::ranges::for_each and simple range-based for loop. No counterparts for std::accumulate, std::reduce or std::inner_product. std::ranges::reduce would be enough, if it were present; inner product can be achieved combining reduce with zip. Falling back to iterator based algorithms is disappointing. Adapting reduce for personal codebase is not a big deal, but a std function is IMHO a lot more desirable. I am wondering if there is such function in std lib or on the 23 horizons.

Regards, FM.

Azoic answered 16/4, 2022 at 13:49 Comment(2)
Small note, but an important one: you can still use std::accumulate() and std::reduce() etc. with ranges. You just have to do r.begin(), r.end().Contemporary
Thanks for remark. I knew that. But what's the point if you still need the begin & end pair? The probability of using unrelated iterators for the pair is a source for bugs.Azoic
S
15

Why is there still no range-enabled reduction algorithm in std?

Because they were not included in "The One Ranges Proposal" P0896 for C++20.

I am wondering if there is such function ... on the 23 horizons.

The expansion of ranges in C++23 has been planned in proposal P2214 "A Plan for C++23 Ranges". The proposal was divided into 3 tiers of priority. Ideally, all tiers would be part of C++23, but that depends on whether there is time for it.

std::ranges::fold is a counterpart to std::accumulate and it It was planned for top tier and has been proposed in P2322 "ranges::fold".

std::ranges::reduce was planned in the middle tier.

std::inner_product counterpart was decided to not be included in the plan for C++23 ranges as fold and reduce were considered sufficient.

can be achieved combining reduce with zip

Zip views themseleves weren't in the C++20 ranges either. But they were planned in the top tier for C++23 and have been proposed in P2321 "zip".

Spermous answered 16/4, 2022 at 14:4 Comment(3)
and the execution policy?Azoic
There was P0836 "Introduce Parallelism to the Ranges TS" prior to C++20 to use executors from P0443 "A Unified Executors Proposal for C++". I doubt either will be in C++23.Spermous
Not having this defeats the purpose of ranges :(Swirl

© 2022 - 2024 — McMap. All rights reserved.