title: boost serialization of stl collection of std unique_ptrs
body: <p>I'd like to be able to serialize stl container of std::unique_ptrs. Can it be done?
  btw, everything works fine with single std::unique_ptr.
  Below is the code I'm working on, and the gcc gives the folowing error: </p>
  
  <pre class="lang-none prettyprint-override"><code> use of deleted function ‘std::unique_ptr&lt;_Tp, _Dp&gt;::unique_ptr(const
   std::unique_ptr&lt;_Tp, _Dp&gt;&amp;) [with _Tp = MyDegrees; _Dp =
   std::default_delete&lt;MyDegrees&gt;; std::unique_ptr&lt;_Tp, _Dp&gt; =
   std::unique_ptr&lt;MyDegrees&gt;]’
  </code></pre>
  
  <p>How can I make the code to work?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;memory&gt;
  #include &lt;fstream&gt;
  #include &lt;map&gt;
  #include &lt;vector&gt;
  #include &lt;boost/serialization/map.hpp&gt;
  #include &lt;boost/serialization/vector.hpp&gt;
  #include &lt;boost/archive/text_oarchive.hpp&gt;
  #include &lt;boost/archive/text_iarchive.hpp&gt;
  #include &lt;boost/archive/xml_oarchive.hpp&gt;
  #include &lt;boost/archive/xml_iarchive.hpp&gt;
  namespace boost {
  namespace serialization {
  
  template&lt;class Archive, class T&gt;
  inline void save(
      Archive &amp; ar,
      const std::unique_ptr&lt; T &gt; &amp;t,
      const unsigned int file_version
  ) {
      // only the raw pointer has to be saved
      const T * const tx = t.get();
      //ar &lt;&lt; tx;
      ar &lt;&lt; boost::serialization::make_nvp("px", tx);
  }
  template&lt;class Archive, class T&gt;
  inline void load(
      Archive &amp; ar,
      std::unique_ptr&lt; T &gt; &amp;t,
      const unsigned int file_version
  ) {
      T *pTarget;
      //ar &gt;&gt; pTarget;
      ar &gt;&gt; boost::serialization::make_nvp("px", pTarget);
  
  #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
      t.release();
      t = std::unique_ptr&lt; T &gt;(pTarget);
  #else
      t.reset(pTarget);
  #endif
  }
  template&lt;class Archive, class T&gt;
  inline void serialize(
      Archive &amp; ar,
      std::unique_ptr&lt; T &gt; &amp;t,
      const unsigned int file_version
  ) {
      boost::serialization::split_free(ar, t, file_version);
  }
  } // namespace serialization
  } // namespace boost
  
  class MyDegrees
  {
  public:
      void setDeg(int d) {
          deg = d;
      }
      int getDeg()const {
          return deg;
      }
  private:
      friend class boost::serialization::access;
      template&lt;class Archive&gt;
      void serialize(Archive &amp; ar, const unsigned int version)
      //{ ar &amp; deg; }
      {
          ar &amp; boost::serialization::make_nvp("DEGS", deg);
      }
      int deg;
  };
  class gps_position
  {
  private:
      friend class boost::serialization::access;
      template&lt;class Archive&gt;
      void serialize(Archive &amp; ar, const unsigned int version)
      //{ ar &amp; degrees;  }
      {
          ar &amp; boost::serialization::make_nvp("DEGS2", degrees);
          ar &amp; boost::serialization::make_nvp("DEGMAP", deg_map);
      }
      std::unique_ptr&lt;MyDegrees&gt; degrees;
      std::vector&lt;std::unique_ptr&lt;MyDegrees&gt; &gt; deg_map;
  public:
      gps_position(): degrees(std::unique_ptr&lt;MyDegrees&gt;(new MyDegrees)) {};
      void setDeg(int d) {
          degrees-&gt;setDeg(d);
      }
      int getDeg() const {
          return degrees-&gt;getDeg();
      }
  };
  
  int TestBasicSerialize(int, char *[])
  {
      int numErr = 0;
      double a;
      std::ofstream ofs("filename");
      gps_position g;
      g.setDeg(45);
      std::cout&lt;&lt;g.getDeg()&lt;&lt;std::endl;
      {
          boost::archive::text_oarchive oa(ofs);
          oa &lt;&lt; g;
      }
      //{ boost ::archive::xml_oarchive oa(ofs); oa &lt;&lt; g;}
      gps_position newg;
      {
          std::ifstream ifs("filename");
          boost::archive::text_iarchive ia(ifs);
          ia &gt;&gt; newg;
          std::cout&lt;&lt;newg.getDeg()&lt;&lt;std::endl;
      }
      return numErr;
  
  }
  </code></pre>