I am trying to make a code snippet generic, but I have trouble doing that as I don't totally understand how the technique used in the code works.
I got the code from another question : C++17 construct array in stack using choosen constructor (same constructor parameter values for each array entry)
Here is the code based on the first response of this post, that is for some reason not working with a size above 88 : http://ideone.com/WDlNwd
#include <iostream>
#include <utility>
struct A1 {
A1() {
printf("A1() called\n");
}
A1(std::size_t i) {
printf("A1(%d) called\n", i);
}
A1(std::size_t i, std::size_t j) {
printf("A1(%d, %d) called\n", i, j);
}
};
struct A2 {
A2(std::size_t i, std::size_t j, double k) {
printf("A2(%d, %d, %lf) called\n", i, j, k);
}
};
template <class T, size_t Size>
class B {
template<typename Arg1, typename ...Args, size_t... Is>
B(std::index_sequence<Is...>, const Arg1 &arg1, const Args &...args) :
tab{ {(void(Is), arg1), args... }... }
{}
public:
T tab[Size];
B() = default;
template<typename ...Args>
B(const Args &...args)
: B(std::make_index_sequence<Size>(), args...) {}
};
int main() {
static constexpr size_t Size = 100;
B<A1, Size> b1(11, 17);
B<A1, Size> b1a(11);
B<A1, Size> b1b;
B<A2, Size> b2(11, 17, 1.2);
return 0;
}
Thanks