[comp.lang.c++] Angry new C++ programmer: GENERIC CODE?

jmork@plains.NoDak.edu (James Mork) (11/24/90)

  I am still digging my way into C++ but I am angry to find no
facilities for generic code--as in Ada.  Can you do this?  Why didn't
they put this in? 

  In Ada, for example, one can create a GENERIC package (class) called--say--
BTREE.  All of the btree operations can be defined and the package 
can be instantiated by the compiler.  What is the best way to do something
like this?  I have Turbo C++ which includes a number of examples and 
a class library.  It would be nice to be able do define a derived class 
class in a similar way as Ada does, however, and just be able to say:


class int_btree : public btree(int) { // int specs type of btree 

-------------
   Assuming you could specifiy a generic class something like this...

class btree(tree_type) {
   tree_type value;
   btree(unsigned maxnodes);
  ~btree(); 
   int add(tree_type value);
   int delete(tree_type value);
   int exists(tree_type value);
...
}

------------------------
  Can you do anything with this effect without having to define a base class
called OBJECT and derive a class BTREE from object... and all of that?
Maybe I am just not deep enough into this yet...

Any response is greatly appreciated
Thanks,

  Jim

-- 
                  UUCP                Bitnet                Internet
          uunet!ndsuvax!numork    numork@ndsuvax     numork@plains.nodak.edu   

avg@hq.demos.su (Vadim G. Antonov) (11/24/90)

In article <6907@plains.NoDak.edu> jmork@plains.NoDak.edu (James Mork) writes:
>  I am still digging my way into C++ but I am angry to find no
>facilities for generic code--as in Ada.  Can you do this?

Yes, of course. The solution of your problem is quite obvious:
USE PREPROCESSOR!!!

Example:

------- file btree.h --------
#ifndef tree_type
!!!!ERROR: tree_type undefined!!!
#endif

#define class_name(x) btree_##x
class class_name(tree_type) {
   tree_type value;
   class_name(tree_type) (unsigned maxnodes);
  ~class_name(tree_type) ();
   int add(tree_type value);
   int delete(tree_type value);
   int exists(tree_type value);
...
}
#undef tree_type
#undef class_name

------- file btree.cc -------
#ifndef tree_type
!!!!ERROR: tree_type undefined!!!
#endif

#define class_name(x) btree_##x

class_name(tree_type)::add(tree_type value)
{
	....
}
.......

-------- Makefile --------
BTREEOBJS=btree.int.o btree.float.o

# Sorry, I'm too lazy to invent something more valuable... :-)
btree.int.o:    btree.h btree.cc
	g++ -c -O -Dtree_type=int btree.cc
	mv btree.o btree.int.o

btree.float.o: btree.h btree.cc
	g++ -c -O -Dtree_type=float btree.cc
	mv btree.o btree.float.o

yourpgm:        $(YOUROBJS) $(BTREEOBJS)

------- Example of usage --------

#define tree_type int
#include "btree.h"

.....

btree_int       *root;


>Why didn't they put this in?

Because C++ assumes separate linkage. If you're allowing use of generic
types you should generate new object code for member functions "on the fly",
it requires recompiling the source code of the non-inline class members.

Moreover, it may cause some strange situations. Let's imagine you've
designed a generic matrix class (say for using with floating-point,
fixed-point, rational and Boolean matrices) and someone is trying to
use your class over his own cell types WITHOUT operation* !

Generic classes in object-oriented languages is a matter of research now
and I think it should not be included in practical language.

>It would be nice to be able do define a derived class
>class in a similar way as Ada does, however, and just be able to say:

It's quite simplier to do in Ada because Ada is far more restrictive
than C++, anyway an implementation of generic type in Ada compiler/linker
is really tricky.

>Maybe I am just not deep enough into this yet...

Do not worry (be happy :-), this is a REAL question. Anyway any problem
in language can be solved at the level of operating system (all machines
are the same and if you can solve problem using one machine/language/
operating system you could do it using another one; time (price :-) is
the only thing to worry about).

Vadim Antonov
DEMOS, Moscow, USSR

PS. I'm sorry I haven't tested this example code. Don't blame.

philip@pescadero.Stanford.EDU (Philip Machanick) (11/25/90)

In article <6907@plains.NoDak.edu>, jmork@plains.NoDak.edu (James Mork) writes:
|>   I am still digging my way into C++ but I am angry to find no
|> facilities for generic code--as in Ada.  Can you do this?  Why didn't
|> they put this in? 

Angry? That's pretty strong. After all, you don't have to program in Ada
any more (calm rational statement designed to defuse yet another language
war). I don't know when the first implementations will appear, but I
believe the next version of C++ will be version 3.0, and the biggest
new feature will be templates, which do what you want.
--
Philip Machanick
philip@pescadero.stanford.edu

davidm@uunet.UU.NET (David S. Masterson) (11/27/90)

>>>>> On 24 Nov 90 00:20:10 GMT, jmork@plains.NoDak.edu (James Mork) said:

James>   I am still digging my way into C++ but I am angry to find no
James> facilities for generic code--as in Ada.  Can you do this?  Why didn't
James> they put this in?

Check the definitions in <generic.h>, if its available on your setup.  Also
look at the definitions of the "template" keyword, again if its available on
your setup.

In effect, pre-C++ 2.0 did not have built-in support for generic objects.  It
relied on the capabilities of CPP to provide what was necessary (see
<generic.h> and Tony Hansen's book for examples).

C++ 2.0 introduced the keyword "template" for generic object definitions.  I
haven't had the chance to use it yet, though.
--
====================================================================
David Masterson					Consilium, Inc.
(415) 691-6311					640 Clyde Ct.
uunet!cimshop!davidm				Mtn. View, CA  94043
====================================================================
"If someone thinks they know what I said, then I didn't say it!"

fuchs@it.uka.de (Harald Fuchs) (11/30/90)

cimshop!davidm@uunet.UU.NET (David S. Masterson) writes:
>C++ 2.0 introduced the keyword "template" for generic object definitions.  I
>haven't had the chance to use it yet, though.

Me too. May be due to the fact that the new keywords are reserved, but
the features aren't yet implemented, not even in AT&T cfront 2.1.

Does anybody know when cfront 3.0 will come out?
--

Harald Fuchs <fuchs@it.uka.de> <fuchs%it.uka.de@relay.cs.net> ...
<fuchs@telematik.informatik.uni-karlsruhe.dbp.de>   *gulp*

jimad@microsoft.UUCP (Jim ADCOCK) (12/04/90)

In article <6907@plains.NoDak.edu>, jmork@plains.NoDak.edu (James Mork) writes:
|>   I am still digging my way into C++ but I am angry to find no
|> facilities for generic code--as in Ada.  Can you do this?  

Well, if your going to be angry, at least apply your heat in the "right"
place!  The ANSI-C++ committee has decided that the templates "generic
classes" facilities are "in" the C++ language.  So now you need to go apply
heat to your C++ compiler vendor to get their compilers up to spec. :-)
See Ellis & Stroustrup "The Annotated C++ Reference Manual"  Addison-Wesley
1990 ISBN 0-201-51459-1 for how templates are suppose to work.

To date, most people have been faking template capabilities using the
C++ macro preprocessor.  A doable, but less than completely satisfactory
approach.

[ Also note that termination-style exceptions as presented in ARM are also
 "in" the C++ language now too. ]