32

I suspect that accumulate isn't the only algorithm that didn't make it.

Maybe now there's a better way to perform accumulation (folding) over a range and therefore the accumulate is obsolete?

GreenScape
  • 7,191
  • 2
  • 34
  • 64
  • What do you mean by (folding) as a better way to perform accumulation? Can you give an example? – BeeOnRope Sep 28 '22 at 06:13
  • 1
    There is no `reduce` at the yet either, https://stackoverflow.com/questions/71894415/why-is-there-still-no-range-enabled-reduction-algorithm-in-std – alfC Dec 19 '22 at 17:01

2 Answers2

33

No, accumulate is a perfectly reasonable algorithm, and it's not made obsolete by any other algorithm. The reason for it not being included in C++20 is simply a matter of time. It was considered better to add as much as possible with regards to ranges, without worrying about adding everything at once. Otherwise, there was a risk that none of the constrained algorithms would have made it to C++20, which would have been a shame.

There are still a few algorithms that haven't been constrained yet, as well as the entirety of the <numeric>, and <memory> headers.

Forunately, there is a proposal to add these remaining algorithms (and I'm optimistic that these will be added in C++23). In fact, the introduction to this proposal answers your question nicely:

“Every time someone asks why we didn’t cover <numeric> and <memory> algorithms: We thought 187 pages of TS was enough.” — Casey Carter

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
cigien
  • 57,834
  • 11
  • 73
  • 112
1

std::ranges::fold_left is the corresponding algorithm, added in C++23. Unfortunately, I don't know of any compiler that already implements it.. However, copying the "possible implementation" from cppreference does work.

ChrisMM
  • 8,448
  • 13
  • 29
  • 48
Yaron Cohen-Tal
  • 2,005
  • 1
  • 15
  • 26