Cumulative Product of Template Parameter Pack

Judge

I'm trying to initialise a static and constant array with the cumulative product of a template parameter pack:

template <int ...D>
class Foo
{
     static const std::array<const Size, sizeof...(D)> _array;
};
template <int...D> const std::array<const int, sizeof...(D)> Foo<D...>::_array = 
{ cumulative_product<D...>() };

How do I write the function cumulative_product<>(), such that it transforms D... into the cumulative product of D...? E.g.

Foo<1,2,3,4>::_array;// = {1,1*2,1*2*3,1*2*3*4} = {1,2,6,24}. 

Solution: Massive thank you to @bogdan for your excellent C++14 solution, and improvements to my C++11 solution.

#include <array>
#include <iostream>

#define CPP_VERSION 11

#if CPP_VERSION >= 14

// Credit: @bogdan at http://stackoverflow.com/q/37373602/6367128
template<int... Args> constexpr std::array<int, sizeof...(Args)> cumulative_product(int seed = 1) { return{ { seed *= Args ... } }; }

#elif CPP_VERSION == 11

// Acknowledgement: Huge thank you to @bogdan for making the code more portable, concise and readable!
namespace details
{
   template<int N, int i, int j, int ...Args> struct c_product_gen               // N counts down to zero
   {
      static constexpr std::array<int, sizeof...(Args)+1> get() { return c_product_gen<N - 1, i*j, Args..., i*j>::get(); }
   };
   template<int i, int j, int ...Args> struct c_product_gen<0, i, j, Args...>    // The end point of the template recursion
   {
      static constexpr std::array<int, sizeof...(Args)+1> get() { return { { j, Args... } }; }
   };
}

template<int... Args> constexpr std::array<int, sizeof...(Args)> cumulative_product() { return details::c_product_gen<sizeof...(Args), 1, Args...>::get(); }

#else // CPP_VERSION < 11

template<int... Args> constexpr std::array<int, sizeof...(Args)> cumulative_product() 
{ 
    static_assert(false, "C++ version 11 or greater is required.");
    return std::array<int, sizeof...(Args)>();
}

#endif

int main()
{
   constexpr auto a = cumulative_product<1,2,3,4,5>();
   for(auto i : a) std::cout << i << ' ';    // Output: 1 2 6 24 120 
   std::cout << '\n';  
}
Piotr Skotnicki
#include <array>
#include <utility>
#include <cstddef>

template <int... D, std::size_t... Is>
constexpr std::array<int, sizeof...(D)> cumulative_product(std::index_sequence<Is...>)
{
    static_assert(sizeof...(D), "Missing initializers");
    int a[]{ D... };
    for (int i = 1; i < int(sizeof...(D)); ++i)
    {
        a[i] *= a[i-1];
    }
    return {{ a[Is]... }};
}

template <int... D>
constexpr auto cumulative_product()
{
    return cumulative_product<D...>(std::make_index_sequence<sizeof...(D)>{});
}

template <int... D>
struct Foo
{
     static constexpr std::array<int, sizeof...(D)> _array  = cumulative_product<D...>();
};

DEMO

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Inspect template parameter pack in gdb

From Dev

Parameter pack with default template argument

From Dev

Specialize on default template parameter pack

From Dev

Dynamically build a template parameter pack

From Dev

a variadic template as a template parameter without pack expansion

From Dev

Function template parameter pack not at the end of the parameter list

From Dev

Deduce template parameter pack from function call

From Dev

Multiple Variadic Parameter Pack for Template Class

From Dev

Remove the last type of a template parameter pack

From Dev

Check if a type is passed in variadic template parameter pack

From Dev

How to fill array with contents of a template parameter pack?

From Dev

Error with variadiac template: "parameter pack must be expanded"

From Dev

Error when trying to expand template parameter pack

From Dev

Template Parameter Pack Fails on Clang but not VS 2015

From Dev

Expand parameter pack in std::function template

From Dev

Template deduction fails with argument after parameter pack

From Dev

How to convert a struct into a template parameter pack?

From Dev

Template Parameter Pack Fails on Clang but not VS 2015

From Dev

template of variadic template: parameter pack expects a type template

From Dev

when template parameter of a template template-parameter is pack expansion, gcc fails, clang succeeds

From Dev

How to extract a value from a variadic template parameter pack by index?

From Dev

C++ using a template parameter pack to invoke multiple templated functions

From Dev

How to extract params from template function parameter pack?

From Dev

Why is this substitution of variadic template parameter failing ? (pack before fixed arguments)

From Dev

Insert/remove type into variadic template list (parameter pack)

From Dev

How can a type be removed from a template parameter pack?

From Dev

Compile time array from C++ template parameter pack

From Dev

Is it possible to use parameter pack to allow template function to accept equivalent types?

From Dev

c++ Unpacking parameter pack from template arguments

Related Related

  1. 1

    Inspect template parameter pack in gdb

  2. 2

    Parameter pack with default template argument

  3. 3

    Specialize on default template parameter pack

  4. 4

    Dynamically build a template parameter pack

  5. 5

    a variadic template as a template parameter without pack expansion

  6. 6

    Function template parameter pack not at the end of the parameter list

  7. 7

    Deduce template parameter pack from function call

  8. 8

    Multiple Variadic Parameter Pack for Template Class

  9. 9

    Remove the last type of a template parameter pack

  10. 10

    Check if a type is passed in variadic template parameter pack

  11. 11

    How to fill array with contents of a template parameter pack?

  12. 12

    Error with variadiac template: "parameter pack must be expanded"

  13. 13

    Error when trying to expand template parameter pack

  14. 14

    Template Parameter Pack Fails on Clang but not VS 2015

  15. 15

    Expand parameter pack in std::function template

  16. 16

    Template deduction fails with argument after parameter pack

  17. 17

    How to convert a struct into a template parameter pack?

  18. 18

    Template Parameter Pack Fails on Clang but not VS 2015

  19. 19

    template of variadic template: parameter pack expects a type template

  20. 20

    when template parameter of a template template-parameter is pack expansion, gcc fails, clang succeeds

  21. 21

    How to extract a value from a variadic template parameter pack by index?

  22. 22

    C++ using a template parameter pack to invoke multiple templated functions

  23. 23

    How to extract params from template function parameter pack?

  24. 24

    Why is this substitution of variadic template parameter failing ? (pack before fixed arguments)

  25. 25

    Insert/remove type into variadic template list (parameter pack)

  26. 26

    How can a type be removed from a template parameter pack?

  27. 27

    Compile time array from C++ template parameter pack

  28. 28

    Is it possible to use parameter pack to allow template function to accept equivalent types?

  29. 29

    c++ Unpacking parameter pack from template arguments

HotTag

Archive