title: Converting a tree-structure in-place to use different pointers at the leaves
body: <p>Suppose I have the following structure:</p>
  
  <pre><code>template&lt;typename LeafData&gt;
  class FooTree {
      FooTree* parent;
      FooTree* children[2];
      LeafData* data; // only present at leaves
  };
  
  template&lt;typename LeafData&gt;
  class FooRoot {
      FooTree* root;
      vector&lt;FooTree&gt; nodeStorage;
      vector&lt;LeafData&gt; dataStorage;
  };
  </code></pre>
  
  <p>I have the following situation in my code:</p>
  
  <pre><code>class HugeData {
      vector&lt;char&gt; hugeAmountsOfData;
  };
  
  class Composite {
      HugeData mainData;
      int someMoreData;
  };
  
  FooRoot&lt;HugeData&gt; root1;
  FooRoot&lt;Composite&gt; root2;
  root2 = magic_cast&lt;HugeData, Composite&gt;(root1);
  </code></pre>
  
  <p>It's the <code>magic_cast</code> implementation I'm struggling with. My current implementation uses a recursive function to rebuild <code>root2</code> with exactly the same node structure as <code>root1</code>, and with data at the leaves created from the first tree's leaves. The problem is that the tree can be huge and takes time to rebuild, whereas I'd simply want an "in-house" conversion that uses as little memory as possible.</p>
  
  <p>One solution I've thought about is something like this (and apologies for the extreme amounts of hacks here):</p>
  
  <pre><code>vector&lt;HugeData&gt; sneak = std::move(root1.dataStorage); // sneak out the leaf data
  root2 = std::move(*reinterpret_cast&lt;FooRoot&lt;Composite&gt;*&gt;(root1));
  vector&lt;Composite&gt; comp;
  for (size_t i = 0; i &lt; sneak.size(); i++)
      comp[i] = Composite{std::move(sneak[i]), 42};
  root2.dataStorage = std::move(comp);
  // root1 is invalid here, for example by passing out of scope
  </code></pre>
  
  <p>This is obviously so hacky it probably doesn't even compile, but you might see the intent here -- to stop treating the tree with one data type as that type and start treating as something else, with some simple code to perform a <code>std::move</code>-ing conversion.</p>
  
  <p>Is there a good way to do this semantically, or is this whole idea just crazy? Thanks!</p>