4

I have a class that contains an array of objects T without a default constructor. Here is an example:

#include <iostream>

struct Param {
    int x;
};

struct A {
    A(const Param &p) {
        std::cout << p.x << std::endl;
    }
};

template<class T, int n>
struct Array {
    T data[n];
                                 /*     n times     */
    Array(const Param &p) : data{/* T(p), ..., T(p) */} { }
};

int main() {
    Param p{42};
    Array<A, 3> arr(p);
    return 0;
}

I am looking for a way to initialize the data field using a varying size initializer list of elements T(p) containing exactly n elements. I have a feeling that it can be achieved by some construction like std::make_index_sequence, but I did not find anything appropriate.

max66
  • 65,235
  • 10
  • 71
  • 111
uranix
  • 657
  • 1
  • 5
  • 23

1 Answers1

7

I suppose you can use a delegating contructor, std::index_sequence and std::make_index_sequence and rewrite Array as follows (Edit: answer improved by SergeyA and Rakete1111; thanks!)

template <typename T, std::size_t N>
struct Array
 {
    T data[N];

    template <std::size_t ... Is>
    Array (Param const & p, std::index_sequence<Is...> const &)
       : data { ((void)Is, T{p}) ... }
     { }

    Array(const Param &p) : Array{p, std::make_index_sequence<N>{}}
     { }
 };

Maybe you can make private the new constructor.

max66
  • 65,235
  • 10
  • 71
  • 111