lundi 28 mars 2016

Initialize std::array by parameter pack from arbitrary index

Initializing an std::array by variadic template arguments, starting from a given index can be done the following way:

#include <array>

template <typename T, size_t N>
struct A
{
   <template ... Ts>
   A(size_t i, Ts ... vals)
   {
      constexpr size_t P = sizeof...(vals);
      std::array<T, P> temp{ vals... };

      for (size_t j = 0; j < P; ++j)
      {
         arr[i + j] = temp[i];
      }
   }

   std::array<T, N> arr;
};

But is it possible to achieve the same without converting the parameter pack to a temporary tuple or another std::array?

Aucun commentaire:

Enregistrer un commentaire