Skip to content

Commit

Permalink
Used safe arithmetic in sum() and product()
Browse files Browse the repository at this point in the history
...at least when using integral types.
  • Loading branch information
tcbrindle committed Aug 28, 2024
1 parent 5d2dce5 commit 5996620
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions include/flux/op/fold.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ struct sum_op {
[[nodiscard]]
constexpr auto operator()(Seq&& seq) const -> value_t<Seq>
{
return fold_op{}(FLUX_FWD(seq), std::plus<>{}, value_t<Seq>(0));
if constexpr (num::integral<value_t<Seq>>) {
return fold_op{}(FLUX_FWD(seq), num::add, value_t<Seq>(0));
} else {
return fold_op{}(FLUX_FWD(seq), std::plus<>{}, value_t<Seq>(0));
}
}
};

Expand All @@ -76,7 +80,11 @@ struct product_op {
[[nodiscard]]
constexpr auto operator()(Seq&& seq) const -> value_t<Seq>
{
return fold_op{}(FLUX_FWD(seq), std::multiplies<>{}, value_t<Seq>(1));
if constexpr (num::integral<value_t<Seq>>) {
return fold_op{}(FLUX_FWD(seq), num::mul, value_t<Seq>(1));
} else {
return fold_op{}(FLUX_FWD(seq), std::multiplies<>{}, value_t<Seq>(1));
}
}
};

Expand Down

0 comments on commit 5996620

Please sign in to comment.