Path: utzoo!utgpu!watmath!att!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!zephyr.ens.tek.com!tekcrl!trost From: trost@crl.labs.tek.com (Bill Trost) Newsgroups: comp.lang.c++ Subject: support for generic classes in c++? Message-ID: <4465@tekcrl.LABS.TEK.COM> Date: 9 Aug 89 22:21:27 GMT Sender: ftp@tekcrl.LABS.TEK.COM Lines: 70 [I was going to mail this solely to the person who asked the question, but I've brought up enough questions in my mind with it that I want to let the rest of the world beat on it] show@risc.com (Sho Kuwamoto) writes: > 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.... ...and then goes on to give definitions, uses, and examples. The question is a good one, and has been discussed far and wide. After multiple inheritance, parameterized types (as they're more commonly called in the C++ community) have been the biggest things on everyone's wish lists (bigger on mine, but maybe my priorities are off :-) ). AT&T is supposed to be working on somesuch. I just hope they don't use the syntax you proposed.... Yes, I do have an alternative syntax; something like: class Stack(X) { X data; Stack(X)* next; public: void Push(); X Pop(); Stack(X)(int flag); // the constructor (I guess) Stack(int flag) // an alternative }; typedef Stack(long) StackOfLongs; typedef Stack(ostream&) StackOfStreams; typedef Stack(streamstack) StackOfStreamStacks; The syntax seems much cleaner this way (except for problems with the constructor syntax), and avoids adding a new keyword. Also, you get a generalization out of this --- there's no reason that paramaterized classes should be restricted to one parameter. A good example would be: class Assoc(left, right) { left name; right value; public: left name(); right value(); Assoc(left,right)(left l, right r); }; typedef Assoc(char*, char*) DictionaryEntry; typedef Assoc(char, (void*)()) KeyTableEntry; Of course, this opens new questions. For instance, can one overload the paramaterized type mechanism, allowing types of the same name with a different number of parameters? Should there be default parameters? What support is there for inheritance among parameterized types? Can I do, for instance: class C(X, Y) : public A(X), public B(Y) {...}; or even class E(X) : public D(F(X)) {...}; The possibilities for semantic perversion are endless. Then again, that's what C++ is all about :-). Bill Trost, Computer Research Labs, Tektronix trost@crl.labs.tek.com / tektronix!crl.labs!trost (trost@reed.bitnet, but probably tektronix!reed!trost)