title: is there a way to pass nested initializer lists in C++11 to construct a 2D matrix?
body: <p>Imagine you have a simple matrix class</p>
  
  <pre><code>template &lt;typename T = double&gt;
  class Matrix {
  
    T* data;
    size_t row, col;
  
  public:
  
    Matrix(size_t m, size_t n) : row(m), col(n), data(new T[m*n]) {}
    //...       
  
    friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const Matrix&amp; m) {
      for (int i=0; i&lt;m.row; ++i) {
        for (int j=0; j&lt;m.col; ++j)
          os&lt;&lt;" "&lt;&lt;m.data[i + j*m.row];
        os&lt;&lt;endl;
      }
      return os;
    }
  };      
  </code></pre>
  
  <p>Is there a way that I can initialize this matrix with an initializer list? I mean to obtain the sizes of the matrix and the elements from an initializer list. Something like the following code:</p>
  
  <pre><code>Matrix m = { {1., 3., 4.}, {2., 6, 2.}};
  </code></pre>
  
  <p>would print </p>
  
  <pre><code> 1 3 4
   2 6 2
  </code></pre>
  
  <p>Looking forward to your answers. Thank you all.
  aa</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>So I worked on your suggestions to craft a somewhat generic array that initializes elements using initializer lists. But this is the most generic I could obtain.
  I would appreciate if any of you have any suggestions as to make it a more generic class. 
  Also, a couple of questions:</p>
  
  <ul>
  <li>Is it fine that a derived class initializes the state of the base class? I'm not calling the base constructor because of this, but should I call it anyways?</li>
  <li>I defined the destructor a the Generic_base class as protected, is this the right way to do it?</li>
  <li>Is there any foreseeable way to carry out the code that belongs to the constructor of the initializer in a more generic way? I mean to have <strong>one</strong> general constructor that takes care of all cases?</li>
  </ul>
  
  <p>I included just the necessary code to illustrate the use of initializer lists in construction. When going to higher dimensions it gets messy, but I did one just to check the code.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cassert&gt;
  
  using std::cout;
  using std::endl;
  
  
  template &lt;int d, typename T&gt;
  class Generic_base {
  
  protected:
  
    typedef T value_type;
  
    Generic_base() : n_(), data_(nullptr){}
  
    size_t n_[d] = {0};
    value_type* data_;
  };
  
  
  
  template &lt;int d, typename T&gt;
  class Generic_traits;
  
  
  template &lt;typename T&gt;
  class Generic_traits&lt;1,T&gt; : public Generic_base&lt;1,T&gt; {
  
  protected:
  
    typedef T value_type;
    typedef Generic_base&lt;1,T&gt; base_type;
    typedef std::initializer_list&lt;T&gt; initializer_type;
  
    using base_type::n_;
    using base_type::data_;
  
  
  public:
  
    Generic_traits(initializer_type l) {
  
      assert(l.size() &gt; 0);
      n_[0] = l.size();
      data_ = new T[n_[0]];
  
      int i = 0;
      for (const auto&amp; v : l)
        data_[i++] = v;
    }
  };
  
  
  template &lt;typename T&gt;
  class Generic_traits&lt;2,T&gt; : public Generic_base&lt;2,T&gt; {
  
  protected:
  
    typedef T value_type;
    typedef Generic_base&lt;2,T&gt; base_type;
  
    typedef std::initializer_list&lt;T&gt; list_type;
    typedef std::initializer_list&lt;list_type&gt; initializer_type;
  
    using base_type::n_;
    using base_type::data_;
  
  public:
  
    Generic_traits(initializer_type l) {
  
      assert(l.size() &gt; 0);
      n_[0] = l.size();
      n_[1] = l.begin()-&gt;size();
  
      data_ = new T[n_[0]*n_[1]];
  
      int i = 0, j = 0;
      for (const auto&amp; r : l) {
  
        assert(r.size() == n_[1]);
        for (const auto&amp; v : r) {
          data_[i + j*n_[0]] = v;
          ++j;
        }
        j = 0;
        ++i;
      }
    }
  };
  
  
  template &lt;typename T&gt;
  class Generic_traits&lt;4,T&gt; : public Generic_base&lt;4,T&gt; {
  
  protected:
  
    typedef T value_type;
    typedef Generic_base&lt;4,T&gt; base_type;
  
    typedef std::initializer_list&lt;T&gt; list_type;
    typedef std::initializer_list&lt;list_type&gt; llist_type;
    typedef std::initializer_list&lt;llist_type&gt; lllist_type;
    typedef std::initializer_list&lt;lllist_type&gt; initializer_type;
  
    using base_type::n_;
    using base_type::data_;
  
  public:
  
    Generic_traits(initializer_type l) {
  
      assert(l.size() &gt; 0);
      assert(l.begin()-&gt;size() &gt; 0);
      assert(l.begin()-&gt;begin()-&gt;size() &gt; 0);
      assert(l.begin()-&gt;begin()-&gt;begin()-&gt;size() &gt; 0);
  
      size_t m = n_[0] = l.size();
      size_t n = n_[1] = l.begin()-&gt;size();
      size_t o = n_[2] = l.begin()-&gt;begin()-&gt;size();
      n_[3] = l.begin()-&gt;begin()-&gt;begin()-&gt;size();
  
      data_ = new T[m*n*o*n_[3]];
  
      int i=0, j=0, k=0, p=0;
      for (const auto&amp; u : l) {
        assert(u.size() == n_[1]);
        for (const auto&amp; v : u) {
          assert(v.size() == n_[2]);
          for (const auto&amp; x : v) {
            assert(x.size() == n_[3]);
            for (const auto&amp; y : x) {
              data_[i + m*j + m*n*k + m*n*o*p] = y;
              ++p;
            }
            p = 0;
            ++k;
          }
          k = 0;
          ++j;
        }
        j = 0;
        ++i;
      }
    }
  };
  
  
  
  template &lt;int d, typename T&gt;
  class Generic : public Generic_traits&lt;d,T&gt; {
  
  public:
  
    typedef Generic_traits&lt;d,T&gt; traits_type;
    typedef typename traits_type::base_type base_type;
  
    using base_type::n_;
    using base_type::data_;
  
    typedef typename traits_type::initializer_type initializer_type;
  
    // initializer list constructor
    Generic(initializer_type l) : traits_type(l) {}
  
    size_t size() const {
      size_t n = 1;
      for (size_t i=0; i&lt;d; ++i)
        n *= n_[i];
      return n;
    }
  
    friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const Generic&amp; a) {
      for (int i=0; i&lt;a.size(); ++i)
        os&lt;&lt;" "&lt;&lt;a.data_[i];
      return os&lt;&lt;endl;
    }      
  };
  
  
  int main()
  {
  
    // constructors for initializer lists
  
    Generic&lt;1, double&gt; y = { 1., 2., 3., 4.};
    cout&lt;&lt;"y -&gt; "&lt;&lt;y&lt;&lt;endl;
  
    Generic&lt;2, double&gt; C = { {1., 2., 3.}, {4., 5., 6.} };
    cout&lt;&lt;"C -&gt; "&lt;&lt;C&lt;&lt;endl;
  
    Generic&lt;4, double&gt; 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&lt;&lt;"TT -&gt; "&lt;&lt;TT&lt;&lt;endl;
  
    return 0;
  }
  </code></pre>
  
  <p>Which prints as expected:</p>
  
  <pre><code>y -&gt;  1 2 3 4
  
  C -&gt;  1 4 2 5 3 6
  
  TT -&gt;  1 4 2 5 3 6 7 10 8 11 9 12 13 16 14 17 15 18 19 22 20 23 21 24
  </code></pre>