Do inside out type erasure.
struct bar_raw
{
std::size_t N;
explicit bar_raw( std::size_t n ):N(n) {}
bar_raw fuse( const bar_raw& rhs ) { return bar_raw(N+rhs.N); }
};
template<size_t N>
struct bar: bar_raw
{
bar():bar_raw(N) {}
template<size_t M>
bar<M+N> fuse(const bar<M>& rhs) { return bar<M+N>(); }
};
Keep all state in bar_raw
. Have a pretty interface that helps writing code in bar<N>
, like n-ary indexes.
When passed a bar<N>
to type erase, you ignore the bar<N>
component and type erase the bar_raw
, which is pretty trivial.
You could call bar_raw
your bar_any
if you wanted, or you could leave bar_raw
as an implementation detail, and wrap it in a bar_any
that is all pretty.