title: C++: How to pass the object from a unique_ptr into a function by value?
body: <p>I would like to use the <code>cereal</code> library from this <a href="https://github.com/USCiLab/cereal" rel="nofollow noreferrer">github page</a> to load <code>xml</code> into objects.
  Up to this point everything is fine.</p>
  <p>But in my application, it is a bit more complex: the object that needs to be loaded/filled by the <code>xml</code> file, have to be accessed through polymorphic pointer.
  Therefore, if a use a raw pointer, the <code>cereal</code> lib refuse to take it, and ask for a smart pointer.
  But, when I give a smart pointer to the generic load <code>cereal</code> function (ie <code>void serialize( Archive &amp; ar )</code> related to <code>cereal::XMLInputArchive::operator()</code>), it tries to load into the whole pointer itself rather than the pointing object.</p>
  <p>Here is an MWE:</p>
  <pre><code>#include &lt;string.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;map&gt;
  #include &lt;memory&gt;
  
  #include &lt;cereal/archives/xml.hpp&gt;
  #include &lt;cereal/types/memory.hpp&gt;
  #include &lt;cereal/types/polymorphic.hpp&gt;
  
  using namespace std;
  
  class SuperParentClass
  {
      public:
          virtual std::string get_nameClass() =0;
          virtual int run() = 0;
          void serialize() ;
  };
  
  class ClassRectangle : public SuperParentClass
  {
      public:
          std::string get_nameClass(){return &quot;Rectangle&quot;;};
          double length=0.;
          double width=0.;
          
          template &lt;class Archive&gt;
          void serialize( Archive &amp; ar )
          {
              ar(  CEREAL_NVP( length )  );
              ar(  CEREAL_NVP( width )  );
          }
  
          int run()
          {
              std::cout &lt;&lt; &quot;I am a Rectangle with length &quot;&lt;&lt;length&lt;&lt;&quot; and width &quot;&lt;&lt;width &lt;&lt; std::endl;
              return 0;
          }
  };
  CEREAL_REGISTER_TYPE(ClassRectangle)
  CEREAL_REGISTER_POLYMORPHIC_RELATION(SuperParentClass, ClassRectangle)
  
  
  int main(void) 
  { 
      // Beginning of main.
      cout &lt;&lt; &quot;(Start)&quot; &lt;&lt; endl;
      
      // Loading from file Part.
      {
          cout &lt;&lt; &quot;Load &quot; &lt;&lt; endl; 
          std::ifstream is(&quot;input.xml&quot;);
          cereal::XMLInputArchive arr(is); 
  
          ClassRectangle Rec;
          arr( Rec ); // or // arr( cereal::make_nvp(&quot;Rectangle&quot;, Rec) );
          Rec.run();
  
          // Now, a bit more complex but closer to my application, because I need a polymorphic pointer !
          std::unique_ptr&lt;SuperParentClass&gt; UPSPC(new ClassRectangle());
          arr( cereal::make_nvp(&quot;Rectangle&quot;, UPSPC ) );  // ask for a polymorphic_id, it does not apply the right function
          // arr( cereal::make_nvp(&quot;Rectangle&quot;, UPSPC.get() ) );  // exempt a smart pointer not a raw pointer
          // arr( cereal::make_nvp(&quot;Rectangle&quot;, &amp;UPSPC ) );  // exempt a smart pointer not a raw pointer
          // arr( cereal::make_nvp(&quot;Rectangle&quot;, std::move(UPSPC) ) );  // ask for a polymorphic_id, it does not apply the right function
          // arr( cereal::make_nvp(&quot;Rectangle&quot;, *UPSPC.get() ) );  // does not compile.
          // arr( UPSPC ); // create an Segmentation Error, because cereal is looking for a polymorphic_id I guess.
          UPSPC-&gt;run();
      }
  
      // End of the main.
      cout &lt;&lt; &quot;(End) &quot; &lt;&lt; endl;   
      return 0; 
  } 
  // EoF
  </code></pre>
  <p>with <code>input.xml</code>:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;cereal&gt;
      &lt;Rectangle&gt;
          &lt;length&gt;2&lt;/length&gt;
          &lt;width&gt;11&lt;/width&gt;
      &lt;/Rectangle&gt;
      &lt;Rectangle&gt;
          &lt;length&gt;12&lt;/length&gt;
          &lt;width&gt;10&lt;/width&gt;
      &lt;/Rectangle&gt;
  &lt;/cereal&gt;
  </code></pre>
  <p>and the signature of <code>make_nvp</code> from <code>cereal.hpp</code>:</p>
  <pre><code>template &lt;class T&gt; inline
   NameValuePair&lt;T&gt; make_nvp( std::string const &amp; name, T &amp;&amp; value )
  {
    return {name.c_str(), std::forward&lt;T&gt;(value)};
  }
  </code></pre>
  <p>The exact error message of the above MWE is :</p>
  <pre><code> terminate called after throwing an instance of 'cereal::Exception'
    what():  XML Parsing failed - provided NVP (polymorphic_id) not found
   Abandon
  </code></pre>
  <p>So my question is: how can I pass the object, from a <code>unique_ptr</code>, into the <code>cereal</code>  function by value ?
  Or is there another way to work around this ?</p>