I have a matrix class with a transposedView()
method that I have been using for years as a "zero overhead" conversion between row and column vectors.
template<int M, int N=M, typename T = double>
struct mat {
std::array<T,M*N> buf;
// ...
template<int Md = M, int Nd = N, typename = std::enable_if_t<Md == 1 || Nd == 1>>
const mat<N, M, T>& transposedView() const {
static_assert(M == 1 || N == 1, "transposedView() supports only vectors, not general matrices.");
return *reinterpret_cast<const mat<N, M, T>*>(this);
}
}
I used to trust this because the memory layout of mat<1,N>
corresponds exactly to mat<N,1>
, but I have just learned that this function has Undefined Behavior. Do you have any advice on what I could replace the contents/implementation of this function with?