Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate initializer list of varying size depending on template parameter

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.

like image 316
uranix Avatar asked Mar 07 '23 00:03

uranix


1 Answers

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.

like image 62
max66 Avatar answered Apr 08 '23 23:04

max66