In c++17 we can easily get the cumulative product of a parameter pack like this:
template<typename... Xs> constexpr std::array<int, sizeof...(Xs)+1> cumulative_product(int x0, Xs... xs) {
return {x0, x0 *= xs ...};
}
constexpr auto cp = cumulative_product(1,2,3,4); // -> 1, 2, 6, 24
Is there a similarly elegant way to get the product of the reverse-ordered arguments -> [4, 12, 24, 24]?
NB: in the original question I had a wrong expected result (24,6,2,1) + imprecise wording of the question. So reversing the output array is not an option here.
Given the same ordering of the input arguments, of course.
I've tried and played around with fold expressions, but it blows up the code size real quick as I couldn't find a way of doing it in the return statement directly.