[llvm] [ADT] Add `sum_of` and `product_of` accumulate wrappers. (PR #162129)

Jakub Kuderski via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 6 11:16:21 PDT 2025


================
@@ -1692,6 +1692,28 @@ template <typename R, typename E> auto accumulate(R &&Range, E &&Init) {
                          std::forward<E>(Init));
 }
 
+/// Wrapper for std::accumulate with a binary operator.
+template <typename R, typename E, typename BinaryOp>
+auto accumulate(R &&Range, E &&Init, BinaryOp &&Op) {
+  return std::accumulate(adl_begin(Range), adl_end(Range),
+                         std::forward<E>(Init), std::forward<BinaryOp>(Op));
+}
+
+/// Returns the sum of all values in `Range` with `Init` initial value.
+/// The default initial value is 0.
+template <typename R, typename E = detail::ValueOfRange<R>>
+auto sum_of(R &&Range, E Init = E{0}) {
----------------
kuhar wrote:

We could do that but I didn't want to hijack shorter names that may already be used elsewhere... The worry is that we could have a name collision with any local function that named `sum` or `product` that takes a range.

https://github.com/llvm/llvm-project/pull/162129


More information about the llvm-commits mailing list