title: C++ Boost serialization of a matrix of vectors
body: <p>I'm using <code>&lt;boost/serializaton&gt;</code>, and I need to serialize a matrix of vectors, member of a class X1, say:</p>
  
  <pre><code>vector&lt;vector&lt;vector&lt;vector&lt;X2&gt;&gt;&gt;&gt; V;
  </code></pre>
  
  <p>where:</p>
  
  <ul>
  <li>V is initialized as V[N][M][S] by the X1's constructor.</li>
  <li>X2 is a class, owning some members and getter/setters, has its own serialize() function (which serializes two std::strings) and it's also initialized by the X1's constr.</li>
  </ul>
  
  <p>The question is: <strong>how can I serialize V?</strong></p>
  
  <p>I tried different approaches but no one worked. I have read the documentation in the Boost site and its examples, but I’m missing something though.
  Here it is the thing:</p>
  
  <pre><code>    #include &lt;boost/archive/binary_iarchive.hpp&gt;
      #include &lt;boost/archive/binary_oarchive.hpp&gt;
      #include &lt;boost/serialization/vector.hpp&gt;
  
      // I tried with the simplest way:
      class X1{
      friend class boost::serialization::access;
      template&lt;class Archive&gt;
      void serialize(Archive&amp; ar, const unsigned int version) {
          ar &amp; V;
          }
      //…
      }; 
  
      // or by calling serialization in a explicit way for each object of V:            
      class X1{ 
      friend class boost::serialization::access;
      template&lt;class Archive&gt;
      void serialize(Archive&amp; ar, const unsigned int version) {
          ar &amp; V;
              for (auto it = V.begin(); it != V.end(); ++it) {
              ar &amp; *it;
                  for (auto jt = (*it).begin(); jt != (*it).end(); ++jt) {
                  ar &amp; *jt;
                      for (auto kt = (*jt).begin(); kt != (*jt).end(); ++kt) {
                      ar &amp; *kt;
                          for (auto wt = (*kt).begin(); wt != (*kt).end(); ++wt) {
                          ar &amp; *wt;
                          }
                      }
                  }
              }
          }
      //…
      }; 
  </code></pre>
  
  <p>I tried the latter (maybe I should do it reversely?) also using ranges, and other different ways too that honestly I have deleted and don’t remember: it serializes but crashes when reading from the serialized file.
  I hope to have expressed myself clearly, it’s my first question. 
  Thanks for the help!</p>