I'm looking for deduction guide for initialize matrix.
I've tried to use pack in pack and sizeof...
, initializer_list<initializer_list<T>>
, custom classes for arrays for construct, but nothing is works...
so, i'm looking for initialize
template <class T, size_t s1, size_t s2>
class matrix
{
T matr[s1][s2]; //maybe with custom array class, if this problem need this
public:
//constructor
};
//deductor
like
matrix m{{1, 2}, {1, 2}};
or
matrix m({1, 2}, {1, 2});
I've found the way to avoid the additional brackets.
This time is required also an explicit deduction guides, SFINAE and an additional method to initialize the matr
member but should works.
The following is a full complete example
#include <iostream>
template <typename T, std::size_t s1, std::size_t s2>
class matrix
{
private:
T matr[s1][s2];
void init_row (std::size_t i, T const (&arr)[s2])
{
for ( auto j = 0u ; j < s2 ; ++j )
matr[i][j] = arr[j];
}
public:
template <std::size_t ... Dims,
std::enable_if_t<sizeof...(Dims) == s1, bool> = true,
std::enable_if_t<((Dims == s2) && ...), bool> = true>
matrix (T const (&...arrs)[Dims])
{ std::size_t i = -1; (init_row(++i, arrs), ...); }
};
template <typename T, std::size_t Dim0, std::size_t ... Dims,
std::enable_if_t<((Dim0 == Dims) && ...), bool> = true>
matrix (T const (&)[Dim0], T const (&...arrs)[Dims])
-> matrix<T, 1u+sizeof...(Dims), Dim0>;
int main()
{
matrix m{{1, 2}, {1, 2}, {1, 2}};
static_assert( std::is_same_v<decltype(m), matrix<int, 3u, 2u>> );
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With