[gnu.g++] support for generic classes in c++?

sho@pur-phy (Sho Kuwamoto) (08/06/89)

     Is there any sensible way that support for generic classes could
be added?  I am new to C++ programming, and have no experience with
compiler design, so this may only be a pipe dream....

     What I mean is, is could C++ be extended to allow users to make a
matrix class that could be used as a matrix of homogeneous objects?
Look at vectors.  While it would be nice to use a constructor with
arguments when making a vector, the fact that you *can't* seems to
make it syntactically easier to extend this type of idea to user
defined generic classes....

     What about a syntax like this?  Define keywords generic and thisType:

******
generic class matrix {
    int n, m;
    thisType *data;
public:
    matrix(int wid, int height) { n=wid; m=height; data = new thisType[n*m]; }
    ~matrix() { delete data; }

    thisType& operator()(int j, int i) { return data[m*j+i]; }
};

class point {
    int x, y;
public:
    point(int startX=0, startY=0) { x = startX; y = startY; }
};

main()
{
    double matrix mDouble(3,4);
    int matrix mInt(3,4);
    point matrix mPoint(5,5);		// makes a 5x5 matrix of points, not
    [...]				// a matrix of points (5,5).
}
******

     Since the types given in the declaration are known at compile
time, it seems that at worst, C++ could make three different classes
(double, int, point) with their names munged.  What about operators?
Imagine that the following operator is a friend of matrix.

******
matrix operator+(matrix& m1, matrix& m2)
{
  matrix out;
  // do some error checking to make sure n&m are the same first.
  for(int i=0; i < m1.n*m1.m; i++)
    out = m1.data[i] + m2.data[i];
}
******

     If a new version of this were made for each type, mPoint+mPoint
would be *legal* if point+point was defined, but mDouble+mInt would
not be ok since the type of the return value is ambiguous.  This is
definitely a problem, but could probably be solved by some slight
changes to how user defined type conversion is done.

******
generic class matrix{
    [...]
public:
    double matrix(int matrix)  { double matrix out;  for(int i=0; ....); }
};
******

You may also need the construct,

******
double matrix operator+(double matrix& m1, int matrix& m2)
{ [...] }
******

in some cases.

     What do you think?  Is this too inefficient?  Should I just use
void* everywhere and do explicit type conversion?  Am I being a
complete idiot?  (most likely) It seems to me that it would be simple
to write this if you already had something which parsed C++ correctly
(ha!).  Just make a symbol table containing each class declared
generic, and make a version of that class for each type you use it
for...

-Sho
--
sho@risc.com  <<-- youth wants to know.