Skip to content

Instantly share code, notes, and snippets.

@rr-codes
Created April 14, 2022 05:09
Show Gist options
  • Save rr-codes/0864669338470e916f3e7daec8d88c2b to your computer and use it in GitHub Desktop.
Save rr-codes/0864669338470e916f3e7daec8d88c2b to your computer and use it in GitHub Desktop.
#include <iostream>
#include <array>
template<typename T, size_t N, size_t M>
class matrix {
public:
template<size_t ... n>
constexpr explicit matrix(const T (&...list)[n]) : _data{}
{
auto pos = &_data[0];
((pos = std::copy(list, list + n, pos)), ...);
}
constexpr matrix() : _data{} {}
constexpr const T& operator()(size_t i, size_t j) const
{
return _data[i * M + j];
}
constexpr T& operator()(size_t i, size_t j)
{
return _data[i * M + j];
}
[[nodiscard]] constexpr auto rows() const
{
return N;
}
[[nodiscard]] constexpr auto cols() const
{
return M;
}
private:
std::array<T, N * M> _data;
};
template<typename T, size_t... n>
matrix(const T (&...list)[n]) -> matrix<T, sizeof...(n), (n + ...) / sizeof...(n)>;
int main() {
constexpr matrix mat2 {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment