<p>There are a couple options here.  I ran into this problem when adding serialization support to trees.  First, you can either replace the references with pointers and then add a default constructor that initializes all the pointers to NULL, or, I think you can add <code>boost::serialization::access</code> as a friend class, and add a private constructor to build from the archive.</p>

<p>The basic problem, as I encountered it (and maybe this is a little bit different) is that a lot of the mlpack code was built with the assumption that the data matrices and other relevant information (like metrics or whatever) were owned by a higher level of the code.  This makes sense when the highest-level code is "load matrix, run method, save results", but as soon as loading and saving models needs to be supported, the problem gets a lot harder, and it needs to be possible for the method to own everything.</p>

<p>Here's an example of what I had to do to the NeighborSearch class to get serialization working, since it internally held a reference to the data matrices:<br>
<a href="https://github.com/mlpack/mlpack/commit/60c81702c432014544351948c091d03deeef6985" class="commit-link"><tt>60c8170</tt></a></p>

<p>I think rvalue references like you mentioned provide a reasonable solution here.  In most places I've done something like provide a const reference constructor that copies the given data, and a move constructor that allows the object to take ownership of what's passed (and therefore avoid the copy).  In this case it seems like making <code>FFN::outputLayer</code> into <code>OutputLayerType</code> instead of <code>OutputLayerType&amp;</code> (just like <code>FFN::layerTypes</code> is of type <code>LayerTypes</code>), like you've suggested, might be the easiest solution.  <a href="https://github.com/zoq" class="user-mention">@zoq</a>'s opinion will probably be more pertinent and useful here, but I thought I'd share my experiences from my (almost finished!) refactoring of all the mlpack methods. :)</p>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">&mdash;<br>Reply to this email directly or <a href="https://github.com/mlpack/mlpack/issues/495#issuecomment-164321866">view it on GitHub</a>.<img alt="" height="1" src="https://github.com/notifications/beacon/AJ4bFHC_-Juy2CdEuN1MNjW4HbRGvAacks5pPhnZgaJpZM4G0YVk.gif" width="1" /></p>
<div itemscope itemtype="http://schema.org/EmailMessage">
<div itemprop="action" itemscope itemtype="http://schema.org/ViewAction">
  <link itemprop="url" href="https://github.com/mlpack/mlpack/issues/495#issuecomment-164321866"></link>
  <meta itemprop="name" content="View Issue"></meta>
</div>
<meta itemprop="description" content="View this Issue on GitHub"></meta>
</div>