I am trying to make a fixed-size matrix class. The intent is to make it inherit or utilize a std::array
of std::array
:
template <size_t Rows, size_t Columns, class T=double>
struct Matrix : public std::array<std::array<T, Columns>, Rows> {
};
I would like to provide an initializer that can automatically deduce the size, like std::array
can in C++17 (which I am using). I'm also fine with using a function to make the Matrix
instead of using class template argument deduction.
// What I want, but does not work:
Matrix matrix {{1., 2.},
{3., 4.}};
// Or:
auto matrix = MakeMatrix ({1., 2.},
{3., 4.});
I have failed to get either of these to be possible. Instead, only the following works:
// Requires specifying the size and repeating `std::array`, both undesired
Matrix<2,2> mat {
std::array{1., 2.},
std::array{3., 4.}
};
// OR this, which requires specifying the size and is generally confusing
Matrix<2,2> mat2 {1., 2.,
3., 4.};
I tried using variadic templates, but this too did not appeal the compiler:
template<class... Args>
auto MakeMatrix (Args... args) {
return Matrix{ std::array {args} ... };
}
// This causes compiler error:
// error: no matching function for call to 'MakeMatrix'
// candidate function [with Args = <>] not viable: requires 0 arguments, but 2 were provided
auto matrix = MakeMatrix ({1., 2.},
{3., 4.});
// This causes compiler error
// error: no viable constructor or deduction guide for deduction of template arguments of 'Matrix'
// note: in instantiation of function template specialization 'MakeMatrix<std::__1::array<double, 2>, std::__1::array<double, 2> >'
// note: note: candidate function template not viable: requires 0 arguments, but 2 were provided
auto matrix = MakeMatrix (std::array {1., 2.},
std::array {3., 4.});
I've also considered using std::initializer_list<std::initializer_list<T>>
, however these do not support fixed size as far as I can tell, and I want the size determined at compile time.
Any thoughts on how to do this, or is it just impossible with the current C++ mechanics?