Re: Returning a template depending on arguments
On 28 Ago, 17:21, Alain Ketterlin <al...@dpt-info.u-strasbg.fr> wrote:
daniele lugli <daniele.lu...@gmail.com> writes:
template<int m, int n>
class SmallMatrix
{
[...]
};
SmallMatrix<m, n1> operator* (const SmallMatrix<n, n1> & f) cons=
t {
[...]
}
This can't work because, for a given (n,m), there must be one
instanciation of op* for each value of n1. You have to make it a
template member, i.e.,
template <int n1> SmallMatrix<m, n1> operator* ...
-- Alain.
Your suggestion works pretty fine:
template<int m, int n>
class SmallMatrix
{
typedef double myData [m] [n];
myData M;
public:
SmallMatrix () {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
M[i][j] = 0.;
}
}
}
virtual ~SmallMatrix () {}
operator myData & () {
return M;
}
operator const myData & () const {
return M;
}
SmallMatrix & operator= (const SmallMatrix & rhs) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
M[i][j] = rhs[i][j];
}
}
return *this;
}
template<int n1>
SmallMatrix<m, n1> operator* (const SmallMatrix<n, n1> & f) const {
SmallMatrix<m, n1> result;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n1; j++) {
result[i][j] = 0.;
for (int k = 0; k < n; k++) {
result[i][j] += M[i][k] * f[k][j];
}
}
}
return result;
}
};
Merci beaucoup!