When can C++17 std::reduce replace std::accumulate, and why can it give a different total for the same data?
answer
- order is unspecified
- associative and commutative
- floating-point addition is neither
- it exists to parallelise
- string concatenation breaks
basics
~20 sstd::reduce may combine elements in any grouping and order, so it gives the same result as std::accumulate only for an associative and commutative operation. Floating-point addition is neither exactly, so totals can differ; non-commutative operations like string concatenation give nondeterministic results.
solid answer
~50 s`std::accumulate` is a strict left fold: `((init + a1) + a2) + ...`, in order. `std::reduce`, added in C++17 with the parallel algorithms, returns a **generalized sum**: the standard lets it regroup the operands and permute them, even in the overload without an execution policy. That freedom lets it split the range across threads or vector lanes, but the result equals `accumulate`'s only when the operation is associative and commutative. Integer `+` qualifies; floating-point `+` does not exactly, so `reduce` over floats can produce a different, sometimes more accurate, total - summing ten million random floats, the in-order float total was off by over one percent while `reduce` with `par` landed much closer. String concatenation is non-commutative, so `reduce` gives nondeterministic output. Also note `reduce(first, last)` defaults its initial value to `value_type{}`, and its parallel form requires forward iterators.
code
cpp · 18 lines#include <cstdio>
#include <execution>
#include <numeric>
#include <random>
#include <vector>
int main() {
std::vector<float> latencyMs(10'000'000);
std::mt19937 gen(1);
std::uniform_real_distribution<float> dist(0.0f, 5.0f);
for (float& x : latencyMs) x = dist(gen);
float inOrder = std::accumulate(latencyMs.begin(), latencyMs.end(), 0.0f);
float anyOrder = std::reduce(std::execution::par, latencyMs.begin(), latencyMs.end(), 0.0f);
double wide = std::reduce(std::execution::par, latencyMs.begin(), latencyMs.end(), 0.0);
std::printf("accumulate %.1f\nreduce %.1f\ndouble %.1f\n", inOrder, anyOrder, wide);
}go deeper
Know both exist in <numeric>: accumulate folds in order, reduce may reorder and can take an execution policy.
Explain the generalized sum, which operations are safe to reorder, and the reduce(first, last) default initial value.
Decide per call site whether reordering is acceptable, catch non-commutative folds and asymmetric lambdas, and use transform_reduce for projected sums.
Set a team policy on floating-point reproducibility: where results must be bit-identical, forbid reordering reductions or fix the grouping explicitly.
## Two different contracts Both live in `<numeric>`, and they look interchangeable: ```cpp auto a = std::accumulate(v.begin(), v.end(), 0.0); auto b = std::reduce(v.begin(), v.end(), 0.0); ``` Their specifications are different: - **`std::accumulate`** initialises `acc` with `init` and applies `acc = op(std::move(acc), *i)` for every element **in order**. It is a left fold, fully sequential. - **`std::reduce`** returns `GENERALIZED_SUM(op, init, *i...)`. The standard defines a generalized sum as the operation applied over **any permutation** of the operands, with **any grouping**. The standard's own note says this yields a nondeterministic result for non-associative or non-commutative operations, naming floating-point addition. This latitude applies to every `reduce` overload, including the one without an execution policy. It is what lets an implementation sum chunks on different threads, or in SIMD lanes, and combine the partial results. ## When the swap is safe | Operation | Associative | Commutative | `reduce` equals `accumulate`? | |---|---|---|---| | `int` / `std::int64_t` addition (no overflow) | yes | yes | yes | | `std::max`, `std::min` | yes | yes | yes | | bitwise `|`, `&`, `^` | yes | yes | yes | | `float` / `double` addition | not exactly | yes | not exactly | | string concatenation | yes | **no** | no: order varies | | subtraction | no | no | no | So for integer totals, maxima and bit masks `std::reduce` is a free upgrade that can be parallelised by adding `std::execution::par`. For anything order-sensitive, keep `std::accumulate` - or use `std::reduce` only after convincing yourself the order does not matter. ## The floating-point surprise Summing ten million `float` latencies in [0, 5) on Apple clang 21 with libc++: 1. `std::accumulate(..., 0.0f)` returned about 24.67 million; 2. `std::reduce(std::execution::par, ..., 0.0f)` returned about 25.00 million; 3. `std::reduce(std::execution::par, ..., 0.0)` (a `double` accumulator) returned about 25.00 million too. The in-order `float` sum is the least accurate here, because once the running total is large each small addend loses most of its bits; splitting into chunks keeps partial sums smaller. The lesson is not that `reduce` is "more correct" - it can also differ run to run or machine to machine - but that **the result depends on order**, and tests that compare floating-point totals with `==` across the two algorithms will fail. Choose the accumulator type deliberately (often `double`) and compare with a tolerance. ## Other differences - **Initial value.** `std::reduce(first, last)` exists and uses `value_type{}`, so it returns the element type; `std::accumulate` always needs `init`, whose type sets the result type. - **Type requirements.** `reduce` mandates that `op(init, *first)`, `op(*first, init)`, `op(init, init)` and `op(*first, *first)` all convert to `T`, because operands may be combined in any pairing. A lambda that only accepts `(T, element)`, as many `accumulate` folds do, makes the program ill-formed. libstdc++ enforces this with `static_assert`s; libc++'s sequential `reduce` is a plain left-to-right loop and compiled such a lambda in a test, so the bug can hide until the code meets the other library. - **Implementations really differ.** That libc++ loop is a legal choice, and so is libstdc++'s: for random-access iterators its sequential `reduce` combines elements in groups of four before folding them into the total. The same call can therefore round differently on two toolchains with no policy anywhere. - **Iterators.** The sequential `reduce` takes input iterators; the policy overloads require forward iterators. - **`transform_reduce`** (C++17) fuses a per-element transformation with the reduction, such as summing the `bytes` field of every record, without an intermediate vector, and has the same ordering freedom. - **Availability of `par`.** With libc++ the policy overloads need `-fexperimental-library`; with libstdc++ they run serially unless TBB is available. ## transform_reduce for record fields Summing one field of a struct is the most common reduction in practice. With `accumulate` you write an asymmetric lambda; with `reduce` that lambda is not allowed. `std::transform_reduce` separates the two concerns: ```cpp std::size_t total = std::transform_reduce( log.begin(), log.end(), std::size_t{0}, std::plus<>{}, [](const LogRecord& r) { return r.bytes; }); ``` The unary lambda projects each record to its `bytes`, and `std::plus<>` combines `size_t` values, which is symmetric, associative and commutative. Adding `std::execution::par` as the first argument parallelises it without changing anything else. ## Deciding in review Ask three questions: is the operation associative and commutative for this type, does any test compare the result exactly, and is the range large enough for parallelism to matter? If the answers are yes, no, yes, `std::reduce` with a policy is the right call. Otherwise `std::accumulate` states the intended order explicitly and costs nothing extra.
- Why is std::reduce with the lambda [](std::size_t acc, const LogRecord& r) { return acc + r.bytes; } wrong when std::accumulate accepts it?`reduce` may combine two elements, two partial sums, or an element and a sum in either order, so the standard mandates that `op(init, *first)`, `op(*first, init)`, `op(init, init)` and `op(*first, *first)` all convert to `T`; a lambda taking `(size_t, const LogRecord&)` covers one form. libstdc++ rejects it with a `static_assert`, while libc++'s sequential loop happens to accept it. Use `std::transform_reduce` with `std::plus<>` and a lambda returning `r.bytes`.
- Is std::reduce without an execution policy deterministic?Not across implementations: every `reduce` overload returns a generalized sum, which allows any grouping and permutation. libc++'s sequential `reduce` folds left to right, while libstdc++'s groups random-access elements in fours, so floating-point totals can differ between them. A given build is usually repeatable, but if the exact order matters, `std::accumulate` is the algorithm that guarantees it.
- Saying reduce is just a faster accumulate with identical results
- Believing reduce without a policy always sums left to right
- Using reduce to concatenate strings in a fixed order
- Comparing float totals from reduce and accumulate with ==
- Assuming any accumulate lambda works unchanged with reduce