// cast 2d to 1d array
template< size_t n, typename T >
T *flatten( T x[][n] ) { return (T *) x; }
// cast 1d to 2d array
template< size_t n, typename T >
T (*unflatten( T x[] ))[n] { return (T (*)[n]) x; }
// (yes, I could have used auto as return type here, but I wanted to give
// you the opportunity to reflect on this syntax for a bit.)
// .... note that C++ isn't to blame for this horror, you can just instantiate
// it for T=float and n=2 to get the same beautiful declaration for unflatten:
float (*unflatten( float x[] ))[2] { return (float (*)[2]) x; }
Comments