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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With