mercredi 16 août 2023

C++11 compile time calculation of constant array

I have class with variadic template parameters like this:

template <uint16_t N1, uint16_t N2, uint16_t... Ns>
class TClass
{
public:
  static constexpr std::size_t S = sizeof...(Ns) + 2;
  static constexpr std::array<uint32_t, S> N = {N1, N2, Ns...};
  static constexpr std::array<uint32_t, S> P= /* struggle */;
};

Two numeric templates parameters are mandatory and more are optional.

I need to fill the array P at compile time with the following properties:

P[0] = 1
P[i] = N[i-1] * P[i-1]

So for example when having a class like this:

TClass instance<3,5,9>;

instance::N is filled with the values {3, 5, 9}

and

instance::P should be filled with the value {1, 3, 15}

Any idea how to generate the sequence for P in C++11 code at compile time?

I tried to use some kind of variadic template parameter unpacking but couldn't find a proper solution.

Aucun commentaire:

Enregistrer un commentaire