Based on the code in this answer, where you got CreateElementFromArray()
from, what you are asking for is simply not possible at all.
CreateElementFromArray()
converts a fixed-sized array of N
values into a sequence of N
parameters passed to T
's constructor at compile-time. For example, this code from that same answer:
struct A {
A(int a, int b) : x(a), y(b) {}
int x, y;
};
int Aargs[] = { 1, 2 };
A* a = createElementFromArray<A>(Aargs);
delete a;
Is translated by the compiler into this code:
struct A {
A(int a, int b) : x(a), y(b) {}
int x, y;
};
int Aargs[] = { 1, 2 };
A* a = new A(Aargs[0], Aargs[1]);
delete a;
That kind of translation is simply not possible with a std::vector
or other dynamic array allocated at runtime.
However, if T
's constructor accepts a vector/array as input, you can use createElement()
from that same answer instead:
template<typename T, typename... TArgs>
T* createElement(TArgs&&... MArgs)
Whatever you pass to createElement()
gets passed as-is to T
' s constructor. For example:
struct A {
A(int* values, size_t N) : values(values, values+N) {}
A(const vector<int> &values) : values(values) {}
vector<int> values;
};
vector<int> Aargs1 = { 1, 2 };
array<int, 3> Aargs2 = { 1, 2, 3 };
int* Aargs3 = new int[4]{ 1, 2, 3, 4 };
A* a = createElement<A>(Aargs1);
delete a;
a = createElement<A>(Aargs1.data(), Aargs1.size());
delete a;
a = createElement<A>(Aargs2.data(), Aargs2.size());
delete a;
a = createElement<A>(Aargs3, 4);
delete a;
delete[] Aargs3;