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

Issue

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.

Solution

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".

Answered By – eerorika

Answer Checked By – Candace Johnson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.