Is it possible to have a generic constructor that accepts any type of initializer list, even if it has nested lists inside?
Suppose you have the following partial template specialization for a class that accepts nested initializer lists in its constructor:
ClassA class template;
template <> class ClassA<4> { typedef std::initializer_list<double> list_type; typedef std::initializer_list<list_type> llist_type; typedef std::initializer_list<llist_type> lllist_type; typedef std::initializer_list<lllist_type> initializer_type; size_t n_[4] = {0}; double* data_; public: ClassA(initializer_type l) { assert(l.size() > 0); assert(l.begin()->size() > 0); assert(l.begin()->begin()->size() > 0); assert(l.begin()->begin()->begin()->size() > 0); size_t m = n_[0] = l.size(); size_t n = n_[1] = l.begin()->size(); size_t o = n_[2] = l.begin()->begin()->size(); n_[3] = l.begin()->begin()->begin()->size(); data_ = new double[m*n*o*n_[3]]; int i=0, j=0, k=0, p=0; for (const auto& u : l) { assert(u.size() == n_[1]); for (const auto& v : u) { assert(v.size() == n_[2]); for (const auto& x : v) { assert(x.size() == n_[3]); for (const auto& y : x) { data_[i + m*j + m*n*k + m*n*o*p] = y; ++p; } p = 0; ++k; } k = 0; ++j; } j = 0; ++i; } } size_t size() const { size_t n = 1; for (size_t i=0; i<4; ++i) n *= n_[i]; return n; } friend std::ostream& operator<<(std::ostream& os, const ClassA& a) { for (int i=0; i<a.size(); ++i) os<<" "<<a.data_[i]; return os<<endl; } }; int main() { ClassA<4> TT = { {{{1.}, {7.}, {13.}, {19}}, {{2}, {8}, {14}, {20}}, {{3}, {9}, {15}, {21}}}, {{{4.}, {10}, {16}, {22}}, {{5}, {11}, {17}, {23}}, {{6}, {12}, {18}, {24}}} }; cout<<"TT -> "<<TT<<endl; return 0; }
This code prints:
TT -> 1 4 2 5 3 6 7 10 8 11 9 12 13 16 14 17 15 18 19 22 20 23 21 24
Now I am trying to generalize the constructor so that I do not have to specialize the class template for each dimension. The problem is that when I replace the constructor with something like:
template <class L> ClassA(std::initializer_list<L> l) { cout<<"generic list constructor"<<endl; }
The clang compiler does not work with an error:
error: no matching constructor for initialization of 'ClassA<4>
Can someone please indicate why this is happening? Matching doesn't work for initializer lists, perhaps because it's a new C ++ feature? Thanks everyone ...
EDIT
Thanks to the help of @ JohannesSchaub-litb and @Daniel Frey, I was able to create a very general constructor that accepts an initializer_list of any dimension. This is the resulting code:
template <int d, typename T> class ClassA { size_t n_[d] = {0}; T* data_; template <int D, typename U> struct Initializer_list { typedef std::initializer_list<typename Initializer_list<D-1,U>::list_type > list_type; Initializer_list(list_type l, ClassA& a, size_t s, size_t idx) { a.n_[dD] = l.size(); size_t j = 0; for (const auto& r : l) Initializer_list<D-1, U> pl(r, a, s*l.size(), idx + s*j++); } }; template <typename U> struct Initializer_list<1,U> { typedef std::initializer_list<T> list_type; Initializer_list(list_type l, ClassA& a, size_t s, size_t i) { a.n_[d-1] = l.size(); if (!a.data_) a.data_ = new T[s*l.size()]; size_t j = 0; for (const auto& r : l) a.data_[i + s*j++] = r; } }; typedef typename Initializer_list<d,T>::list_type initializer_type; public:
Of course the code prints
TT -> 1 4 2 5 3 6 7 10 8 11 9 12 13 16 14 17 15 18 19 22 20 23 21 24
I love this metaprogramming pattern! Thanks guys for helping with this.
aa