Advertisement
zmatt

C declarations ♥

Apr 3rd, 2020
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.35 KB | None | 0 0
  1. // cast 2d to 1d array
  2. template< size_t n, typename T >
  3. T *flatten( T x[][n] ) {  return (T *) x;  }
  4.  
  5. // cast 1d to 2d array
  6. template< size_t n, typename T >
  7. T (*unflatten( T x[] ))[n] {  return (T (*)[n]) x;  }
  8. // (yes, I could have used auto as return type here, but I wanted to give
  9. // you the opportunity to reflect on this syntax for a bit.)
  10.  
  11.  
  12.  
  13.  
  14. // .... note that C++ isn't to blame for this horror, you can just instantiate
  15. // it for T=float and n=2 to get the same beautiful declaration for unflatten:
  16. float (*unflatten( float x[] ))[2] {  return (float (*)[2]) x;  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement