This is the next question: std :: initializer_list as constructor std :: array
I found out that I had a bit rejected the answer I received and tried to expand the shell class a bit. It briefly says the problem, my solution and the problems with it.
Problem : I need a class that creates an array from a template without a default constructor and an unknown number of arguments. It can itself be wrapped from other classes with variable patterns.
My solution : it works, but really not very friendly, so I hope for some ideas to improve it. Example of execution
class a // some sort of storage class that need wrapper for more functions { public: a(int c) { std::cout << "Class A: " << c << std::endl; } }; template<class TStack> class b : public TStack // wrapper with char input { public: template<class ... TArgs> b(char c, TArgs&& ... args) : TStack( std::forward<TArgs>(args)...) { std::cout << "Class B: " << c << std::endl; } }; template<class TStack, unsigned int TBin> class c // array wrapper { private: template<class TStack> class d : public TStack // tuple Expansion { private: template <class ... TArgs, std::size_t... Is> d(std::tuple<TArgs...> args, std::index_sequence<Is...>) : TStack(std::get<Is>(args)...) {} public: template<class ... TArgs> d(std::tuple<TArgs...> args) : d(args, std::make_index_sequence< sizeof...(TArgs) >{}) {} }; std::array< d<TStack>, TBin> data; template <class TArgs, std::size_t... Is> c(std::initializer_list< TArgs > il, std::index_sequence<Is...>) : data{ { *(il.begin() + Is)... } } { } public: template<class TArgs> c(std::initializer_list< TArgs > il) : c(il, std::make_index_sequence<TBin>{}) {} }; int main() { a(1); b<a>('a', 3); c<a, 3>({ std::make_tuple(4), std::make_tuple(5), std::make_tuple(6)}); b< c<a, 4> >('a', std::initializer_list<std::tuple<int>>{std::make_tuple(7), std::make_tuple(8), std::make_tuple(9), std::make_tuple(10)}); c< b<a>, 4>({ std::make_tuple('A', 11), std::make_tuple('B', 12), std::make_tuple('C', 13), std::make_tuple('D', 14) }); b< c< b<a>, 4>>('z', std::initializer_list<std::tuple<char, int>>{ std::make_tuple('A', 11), std::make_tuple('B', 12), std::make_tuple('C', 13), std::make_tuple('D', 14) }); }
Why I donβt like my solution : The code works fine, but std::initializer_list<std::tuple</*lot of types*/>> infront of each list is not the most user-friendly way I hope for. make_tuple is fine, but slightly reduces readability for large lists.
Regards Matyro