I want to pass a row-major argument list to a constexpr constructor and store the data in a column-major array. I have to transpose the template parameter pack from row-major to column-major and the only way I came up with was by using a temporary array like this:
template <typename T, std::size_t rows, std::size_t cols = rows>
class Matrix final
{
public:
std::array<T, cols * rows> m; // column-major matrix
template <typename ...A>
constexpr Matrix(const A... args) noexcept:
m{transpose(std::array<T, rows * cols>{args...}, std::make_index_sequence<rows * cols>{})}
{}
template <std::size_t ...i>
constexpr auto transpose(const std::array<T, cols * rows> a,
const std::index_sequence<i...>) noexcept
{
return std::array<T, cols * rows>{a[((i % rows) * cols + i / rows)]...};
}
};
I guess there is no other way to access the elements in a parameter-pack out of order, but maybe there is a way to do it without an intermediate array.