Not sure how to do that for an arbitrary variable, but in context of a function template such argument can be built with template recursion:
template<typename T, std::size_t Pos = 0, std::size_t Size, typename F, typename ...Elements>
void makeArray(const F(&arr)[Size], const Elements&... elements) {
if constexpr(Pos < Size) {
makeArray<T, Pos + 1>(arr, elements..., arr[Pos]);
} else {
const T newArray[] { T(elements)... };
std::cout << sizeof(newArray) << std::endl;
}
}
You can extend it to a class member, but unfortunately such a recursion doesn't quite work for constructors and you will have to deal with a free function factory (named constructor idiom neither works if you don't want to specify all the template arguments in advance):
template<typename Type, std::size_t Size>
class my_class {
const Type array[Size];
template<typename... Elements>
my_class(const Elements&... elements): array{ elements... } {}
template<typename T, std::size_t Pos, std::size_t S, typename F, typename ...Elements>
friend my_class<T, S> makeMyClass(const F(&arr)[S], const Elements&... elements);
};
template<typename T, std::size_t Pos = 0, std::size_t S, typename F, typename ...Elements>
my_class<T, S> makeMyClass(const F(&arr)[S], const Elements&... elements) {
if constexpr(Pos < S) {
return makeMyClass<T, Pos + 1>(arr, elements..., arr[Pos]);
} else {
return my_class<T, S>{ elements... };
}
}
This is how it looks from the client code:
int main(){
const char DICTIONARY[][6] = {
"apple",
"sands"
};
const my_class instance = makeMyClass<LetterSet>(DICTIONARY);
return 0;
}