Re: Returning a template depending on arguments
On 29 Ago, 01:15, Noah Roberts <roberts.n...@gmail.com> wrote:
Not true.
My minimal example offers matrix multiplication; matrix-vector
multiplication (over a similar SmallVector template class) and other
similar operations can be added.
The compiler checks that such operations are made with the correctly
sized types.
And none of that adds anything. I can do all that with the array.
Of course yes; and it's precisely what I don't want to do. I don't
like copying and pasting the same code many times.
With the risk of pasting the wrong lines and looping beyond the size
of the matrix, what is impossible with my class.
In
fact, I'd be better off doing so since I'm not relying on silent
conversion operators to break encapsulation.
your opinion.
The encapsulation
benefit you'd normally get from implementing a matrix class is
entirely destroyed by your conversion operator,
I have no reason to completely hide the data: accessing directly via []
[] is a simple and overhead-free way to fill or read the entries.
Then why not just leave the variable public?
Why should I write .M[][] when I can simply write [][] ?
an obviously misjudged
attempt to force [][] syntax into the equation.
Why 'attempt'? It works.
Does it?
Yes it does.
Quite the contrary in
fact, your class provides for some interesting avenues for unexpected
behavior.
Examples please.
As with all classes that offer implicit conversion operators, your
conversion sequence will be used silently when perhaps it should not.
Are you able to show an example of this? Code, I mean, not words.
Reconsider your conversion operator and attempt to gain [][] syntax.
Maybe through an operator[] returning a row selector object, having in
turn an operator[] ?
Or, I don't know, a function?
Give your example code please.
This could leave place to add some bound-checking code.
But I don't need bound-checking here and I don't want the overhead of
this mechanism.
Because you've measured it and know that it's a problem, right?
Yes, of course. Here is the code, running on Linux:
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;
}
};
template<int m, int n>
class OtherSmallMatrix
{
typedef double myData [m] [n];
myData M;
//template<int m, int n>
class OtherSmallMatrixRowSelector
{
myData & data;
int row;
public:
OtherSmallMatrixRowSelector (OtherSmallMatrix & _osm, int
_row):
data (_osm.M), row (_row) {}
OtherSmallMatrixRowSelector (const
OtherSmallMatrixRowSelector & as):
data (as.data), row (as.row) {}
double & operator[] (int col) { return data[row][col]; }
};
public:
OtherSmallMatrix () {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
M[i][j] = 0.;
}
}
}
virtual ~OtherSmallMatrix () {}
OtherSmallMatrixRowSelector operator[] (int row) {
return OtherSmallMatrixRowSelector (*this, row);
}
};
#include <sys/time.h>
#include <sys/resource.h>
#include <iostream>
using namespace std;
int main () {
struct rusage before, after;
double diff;
const int repeat = 1000000;
SmallMatrix<2, 3> sm;
getrusage (RUSAGE_SELF, &before);
for (int k = 1; k <= repeat; k++) {
sm[1][1] = (double)k;
}
getrusage (RUSAGE_SELF, &after);
diff = (after.ru_utime.tv_sec - before.ru_utime.tv_sec) +
(after.ru_utime.tv_usec - before.ru_utime.tv_usec) * 1.e-6;
cout << "Time in seconds for " << repeat << " accesses to a
SmallMatrix element: "
<< diff << endl;
OtherSmallMatrix<2, 3> osm;
getrusage (RUSAGE_SELF, &before);
for (int k = 1; k <= repeat; k++) {
osm[1][1] = (double)k;
}
getrusage (RUSAGE_SELF, &after);
diff = (after.ru_utime.tv_sec - before.ru_utime.tv_sec) +
(after.ru_utime.tv_usec - before.ru_utime.tv_usec) * 1.e-6;
cout << "Time in seconds for " << repeat << " accesses to an
OtherSmallMatrix element: "
<< diff << endl;
}
and here are the results on my machine:
Time in seconds for 1000000 accesses to a SmallMatrix element: 0.004
Time in seconds for 1000000 accesses to an OtherSmallMatrix element:
0.008
Doubling the time could really be a problem.
What I want is just to avoid rewriting the loops for matrix
operations.
And...?