Is it a way in c++17 to construct an array in stack using another constructor than the default constructor.
This is a special case when each array value is constructed with the same constructor parameters.
I need to use a basic stack located array (not a vector or an array of pointers or something else).
This code illustrate what I would like to do :
#include <iostream>
using namespace std;
struct A1 {
A1() {
printf("A1() called\n");
}
A1(int i, int j) {
printf("A1(%d, %d) called\n", i, j);
}
};
struct A2 {
A2() {
printf("A2() called\n");
}
A2(int i, int j, double k) {
printf("A2(%d, %d, %lf) called\n", i, j, k);
}
};
template <class T>
struct B {
T tab[100];
template<class... Args>
B(Args&&... objectArgs) {
// I would like to call T(std::forward<Args>(objectArgs)...) for each tab entries instead of the default constructor
// In B we don't know all the possible T this is why a variadic constructor is used
}
};
int main() {
B<A1> b1(1, 7);
B<A2> b2(1, 7, 1.2);
return 0;
}
Thanks
Aucun commentaire:
Enregistrer un commentaire