1. Problem It Solves
Numeric workloads need reductions that may be reorganized or executed under policies, plus fused transform-and-reduce operations such as dot products. C++17 standardizes both.
This lesson reduces that broad problem to one fixed-input program so the language rule and its observable result can be checked independently.
2. Prerequisites
A C++17 compiler invoked with warnings enabled and the earlier lessons listed in the course order.
Know
std::accumulate, associativity, floating-point rounding, iterators, and binary operations.
3. Core Idea
std::reduce combines a range and may group operations in an unspecified order. std::transform_reduce first combines corresponding inputs through a transform and then reduces those results, potentially enabling fusion and parallel execution.
Keep the type, object lifetime, ownership, and evaluation boundary visible while reading the example; syntax is useful only when those semantics are understood.
4. Minimal Syntax
auto total = std::reduce(first, last, init);
auto dot = std::transform_reduce(a.begin(), a.end(),
b.begin(), 0);5. How It Works
One integer vector is reduced to a sum, then two vectors are transformed pairwise by multiplication and reduced.
Integer addition stays exact within range, so unspecified regrouping cannot change these fixed results.
The program prints
sum: 10anddot: 70, giving a small test oracle that can be compared with the prediction made before compilation.
6. Common Mistakes
Non-associative operations or floating-point addition can produce different results when reduction order changes; side effects in operations are unsafe assumptions.
A successful build is not proof of correct semantics. Recheck lifetimes, invalidation, ordering, error paths, and required headers or link flags for the real program.
7. When to Use It
Use this technique when a workload is mathematically reducible and operation regrouping is acceptable.
Choose a simpler C++11/14 form when the C++17 rule does not improve safety, clarity, or measured performance for the supported toolchains.
8. Simple Example
The dot product multiplies corresponding elements and adds them without an intermediate vector. Small integers keep overflow out of the lesson.
The companion .cpp file has no input or external dependency. Predict the complete output, compile it, run it, then change one constant and explain the new result.
Complete sample code
Source file
cpp17/37_reduce_transform_reduce/main.cpp
#include <iostream>
#include <numeric>
#include <vector>
int main() {
const std::vector<int> left{1, 2, 3, 4};
const std::vector<int> right{5, 6, 7, 8};
const int total = std::reduce(left.begin(), left.end(), 0);
const int dot = std::transform_reduce(
left.begin(), left.end(), right.begin(), 0);
std::cout << "sum: " << total << '\n';
std::cout << "dot: " << dot << '\n';
}
9. Key Takeaways
Reduction permits reordering; choose operations and numeric types whose semantics tolerate that freedom.
C++17 mode must be selected explicitly; a newer compiler default can otherwise hide a portability error.
Warnings, deterministic examples, and small assertions turn a remembered rule into evidence.
Document any lifetime, ownership, synchronization, or allocation contract at the API boundary.
10. Self-Check Questions
Easy — What problem does std::reduce and std::transform_reduce address?
Medium — Which pairwise products contribute to the dot product?
Hard — Why can
reduceandaccumulatedisagree for floating-point data?