title: Serializing a map of objects to xml using boost::serialization
body: <p>The serialization example below is from the <a href="http://lists.boost.org/boost-users/2007/01/24537.php" rel="nofollow noreferrer">boost mailing list</a> which is pretty much the same as what I would like to do. However, I have changed the archive so that it will serialize to XML. The compile does not fail if I serialize to binary, but it fails when serializing to xml. The compile fails in <code>basic_xml_oarchive.hpp</code> in the following method:</p>
  
  <pre><code>// boost code where compile fails
  template&lt;class T&gt;
  void save_override(T &amp; t, BOOST_PFTO int)
  {
      // If your program fails to compile here, its most likely due to
      // not specifying an nvp wrapper around the variable to
      // be serialized.
      BOOST_MPL_ASSERT((serialization::is_wrapper&lt;T&gt;));
      this-&gt;detail_common_oarchive::save_override(t, 0);
  }
  </code></pre>
  
  <p>It seems I haven't done enough to allow the <code>std::map&lt;int, CSomeData&gt;</code> object to be serialized, any ideas on how to fix this?</p>
  
  <hr>
  
  <p>My serialization implementation:</p>
  
  <pre><code>#include &lt;boost/archive/xml_oarchive.hpp&gt;
  #include &lt;boost/archive/xml_iarchive.hpp&gt;
  #include &lt;boost/serialization/map.hpp&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;map&gt;
  
  using namespace std;
  
  // This is a test class to use as the map data.
  class CSomeData {
      public:
          CSomeData(){};
          CSomeData(float f0, string str0)
          {
              m_f0 = f0;
              m_str0 = str0;
          }
  
          float m_f0;
          string m_str0;
  
      private:
          friend class boost::serialization::access;
  
          template&lt;class Archive&gt;
          void serialize(Archive &amp;ar, const unsigned int version)
          {
              ar &amp; m_f0;
              ar &amp; m_str0;
          }
  };
  
  // This is the class we really want to try serializing.
  class CTest {
      public:
          CTest(){};
          CTest(int nNumber)
          {
              m_nNumber = nNumber;
  
              // Fill with some dummy data.
              m_mTst.insert(make_pair(0, CSomeData(0.23f, "hi hi hi")));
              m_mTst.insert(make_pair(1, CSomeData(7.65f, "second one")));
              m_mTst.insert(make_pair(2, CSomeData(9.23f, "third one")));
              m_mTst.insert(make_pair(3, CSomeData(5.6766, "chosen one")));
          }
          ~CTest(){};
  
          save()
          {
              std::ofstream ofs("filename");
  
              // Write class instance to archive. Writing seems to work ok.
              boost::archive::xml_oarchive oa(ofs);
              oa &lt;&lt; BOOST_SERIALIZATION_NVP(*this);
          }
  
          int m_nNumber;
  
      private:
          map&lt;int, CSomeData&gt; m_mTst;
  
          friend class boost::serialization::access;
  
          template&lt;class Archive&gt;
          void serialize(Archive &amp;ar, const unsigned int version)
          {
              ar &amp; m_nNumber;
              ar &amp; m_mTst;
          }
  };
  </code></pre>