Let's say I have two unrelated types that are exactly the same size.
struct Foo { ... };
struct Bar { ... };
And now, let's say that I have a tree of Foo that I would like to convert to a tree of Bar.
template <typename Leaf>
struct Tree
{
Leaf data;
std::unique_ptr<Tree> lhs;
std::unique_ptr<Tree> rhs;
};
Tree<Foo> footree = ...;
Tree<Bar> bartree = convert(std::move(footree)); // <--
How could I perform this conversion while reusing the heap-allocated memory of footree? In other words, how can I (safely) convert a Tree<Foo> to a Tree<Bar> with no extra memory allocations?
This is a simplified example of what I'm actually trying to do. In reality Foo and Bar are recursive variants with differing numbers of alternative types.