generic matrix template

This commit is contained in:
hladu357 2024-05-21 18:32:26 +02:00
parent a1a0bc1a2f
commit 223c486c92
1 changed files with 64 additions and 0 deletions

64
m.cpp Normal file
View File

@ -0,0 +1,64 @@
template<class T, unsigned ... RestD> struct Matrix;
template<class T, unsigned FirstDim >
struct Matrix<T, FirstDim> {
typedef T GenericDimType[FirstDim];
GenericDimType data;
inline T& operator[](unsigned i) { return data[i]; }
};
template<class T, unsigned FirstDim, unsigned ... RestD >
struct Matrix<T, FirstDim, RestD...> {
typedef typename Matrix<T, RestD...>::GenericDimType FirstDimType;
typedef FirstDimType GenericDimType[FirstDim];
GenericDimType data;
inline FirstDimType& operator[](unsigned i) { return data[i]; }
};
#include <iostream>
int main() {
Matrix<int, 2, 3, 4> m;
int cnt = 0;
for (size_t i = 0; i < 2; ++i) {
for (size_t j = 0; j < 3; ++j) {
for (size_t h = 0; h < 4; ++h) {
m[i][j][h] = cnt++;
}
}
}
std::cout << "Data in matrix:\n";
for (size_t i = 0; i < 2; ++i) {
for (size_t j = 0; j < 3; ++j) {
for (size_t h = 0; h < 4; ++h) {
std::cout << m[i][j][h] << ' ';
}
std::cout << '\n';
}
std::cout << "ENDDIM\n";
}
std::cout << "Addresses in matrix:\n";
for (size_t i = 0; i < 2; ++i) {
for (size_t j = 0; j < 3; ++j) {
for (size_t h = 0; h < 4; ++h) {
std::cout << &m[i][j][h] << ' ';
}
std::cout << '\n';
}
std::cout << "ENDDIM\n";
}
return 0;
}