title: Boost serialization of class wrapping a pointer
body: <p>I have a class <code>Ptr</code> which wraps a pointer.
  This pointer can handle structures such as <code>Ptr&lt;A&gt;</code> where <code>A</code> can be a complex structure or primitives such as <code>Ptr&lt;double&gt;</code>. I would like to specify the <code>save</code> and <code>load</code> functions of <code>Ptr</code> serilaization to work for both of these cases.<br>
  Here I try to reproduce a simplified example :</p>
  
  <pre><code>struct A { 
      A(int aa) : a(aa) {} 
      int a; 
      template&lt;class Archive&gt;
      void serialize(Archive &amp;ar, const unsigned int version) {
          ar &amp; BOOST_SERIALIZATION_NVP(a);
      }
  };
  
  template &lt;typename T&gt;
  struct Ptr {
      Ptr() : m_elem(0) {}
      Ptr(const T* elem) { m_elem = (elem ? new T(*elem) : 0); };
      const T&amp; operator*() const { return *m_elem; };
      T&amp; operator*()  { return *m_elem; };
      const T* operator-&gt;() const { return m_elem;};
      T* operator-&gt;() { return m_elem;};
  
      // [...] other ctor, copy, del, etc...
  
      T* m_elem;
  };
  
  namespace boost { namespace serialization {
  
  template&lt;class Archive, class T&gt;
  void save(Archive &amp; ar, const Ptr&lt;T&gt; &amp;ptr, const unsigned int version) {
      T* base_pointer = (ptr.m_elem);
      ar &amp; boost::serialization::make_nvp("Ptr", base_pointer);
  }
  
  template&lt;class Archive, class T&gt;
  void load(Archive &amp; ar, Ptr&lt;T&gt; &amp;ptr, const unsigned int version) {
      T *base_pointer;
      ar &amp; boost::serialization::make_nvp("Ptr", base_pointer);
      ptr.m_elem = base_pointer;
  }
  
  template&lt;class Archive, class T&gt;
  void serialize(Archive &amp; ar, Ptr&lt;T&gt; &amp;ptr, const unsigned int version)
  {
      boost::serialization::split_free(ar, ptr, version);
  }
  
  }} // end namespace
  
  int main() {
      Ptr&lt;A&gt; p1(new A(4));
      std::cout &lt;&lt; p1.m_elem-&gt;a &lt;&lt; std::endl;
      Ptr&lt;double&gt; p2(new double(2.0));
      std::cout &lt;&lt; *(p2.m_elem) &lt;&lt; std::endl;
  
      // The serialization for Ptr&lt;A&gt; seems to work
      std::ostringstream archive_ostream;
      boost::archive::xml_oarchive oa(archive_ostream);
      oa &lt;&lt; BOOST_SERIALIZATION_NVP(p1); 
      std::cout &lt;&lt; archive_ostream.str() &lt;&lt; std::endl;
  
      // Serialization for Ptr&lt;double&gt; does not compile
      /*
      std::ostringstream archive_ostream2;
      boost::archive::xml_oarchive oa2(archive_ostream2);
      oa2 &lt;&lt; BOOST_SERIALIZATION_NVP(p2); 
      std::cout &lt;&lt; archive_ostream2.str() &lt;&lt; std::endl;
      */
  }
  </code></pre>
  
  <p><a href="http://coliru.stacked-crooked.com/a/97fba0bd1275e589" rel="nofollow">Live example</a></p>
  
  <p>As you can see, the serialization for <code>Ptr&lt;A&gt;</code> seems to work (still I am not sure if it is safe enough). However, serialization for <code>Ptr&lt;double&gt;</code> is not compiling.</p>
  
  <p>Error output is : </p>
  
  <blockquote>
    <p>main.cpp: In instantiation of 'void
    boost::serialization::save(Archive&amp;, const Ptr&amp;, unsigned int)
    [with Archive = boost::archive::xml_oarchive; T = A]':</p>
    
    <p>/usr/local/include/boost/serialization/split_free.hpp:45:13:<br>
    required from 'static void boost::serialization::free_saver::invoke(Archive&amp;, const T&amp;, unsigned int) [with Archive =
    boost::archive::xml_oarchive; T = Ptr]'</p>
    
    <p>/usr/local/include/boost/serialization/split_free.hpp:74:18:<br>
    required from 'void boost::serialization::split_free(Archive&amp;, T&amp;,
    unsigned int) [with Archive = boost::archive::xml_oarchive; T =
    Ptr]'</p>
    
    <p>main.cpp:57:34:   required from 'void
    boost::serialization::serialize(Archive&amp;, Ptr&amp;, unsigned int) [with
    Archive = boost::archive::xml_oarchive; T = A]'</p>
  </blockquote>
  
  <p>Hence I am looking for a correct <code>Ptr</code> serialization !</p>