The std::inner_product is a left-fold reduction algorithm that, in each step, first reduces the current elements from both ranges into a single value and then folds the result into an accumulator. The reduction and accumulation operations can be customized and, due to the strict left-to-right operation, are allowed to have state and side effects. Compiler Explorer link: https://lnkd.in/df_Ahgfa #cpp #cplusplus #coding #programming #dailybiteofcpp
Cool especially the second example is very useful also for using accumulate. Didn't occur to me how useful it is to have a different accumulating type than what is iterated over!
If I'd seen something like this in PR, I probably wouldn't approve. While this might be useful in some cases, the above example could be better rewritten as a single for loop and even being faster depending on the optimizations. We should prioritize code readability in cases when it's not required otherwise.