Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!chaph.usc.edu!alcor.usc.edu!jeenglis From: jeenglis@alcor.usc.edu (Joe English) Newsgroups: comp.lang.c++ Subject: Re: Generics in C++ Keywords: generic Message-ID: <16476@chaph.usc.edu> Date: 9 Apr 91 22:36:06 GMT References: <1410@h.cs.wvu.wvnet.edu> Sender: news@chaph.usc.edu Organization: A child, an elderly man, a Cuban Lines: 53 Nntp-Posting-Host: alcor.usc.edu In-Reply-To: schiebel@cs.wvu.wvnet.edu (Darrell Schiebel) schiebel@cs.wvu.wvnet.edu (Darrell Schiebel) writes: >We are designing a reuseable software library based on C++ components. >Currently we are exploring the various (twisted) ways of implementing >a "generics" layer on C++. The alternatives seem to be: > 1) Pointer Based Objects on top of C++ (Smalltalkish) > 2) Pre-processor based macro generics with type substitution and > using the "generic header file." > 3) A yacc/sed/awk preprocessor for implimenting the generics > ((C++)++) >Does anyone out there have any experience with these mechanisms? or better >yet Does anyone have a better solution? I've had some success with solution #2 (in C, with a generic hash table and a generic AVL tree), but not the way that Stroustrup does it in "The C++ Programming Language". That method involves writing separate "declare_XXX(...)" and "implement_XXX(...)" macros for generic types, which I found distasteful since the macros tend to be long, multi-line beasts and the implementations have to use lots of casts to and from (void *). The scheme I used was something like this: #define AVL_BASETYPE int #define AVL_COMPARE(x,y) (x < y) /* ... other generic parameters ... */ #include "avl.gc" where avl.gc defines the relevant datatypes for AVL trees and (static) functions performing all the operations on them. The code was designed to be used as the implementation of a higher-level data structure (a symbol table, say); everything defined in the ".gc" file is only visible in the file which includes it, which in turn exports functions of its own to the rest of the program. This somewhat limits the useability of the generic code, but it worked pretty well for my purposes. I can probably dig up the source code if you'd like to take a look at it. Alternately, gcc has a pretty decent generic mechanism using #3, or you could just wait until current compilers implement section 14 of the ARM (or whatever that section ends up as when the revisions are complete...) What I'd really like to see in C++ are true polymorphic functions, a la SML and Haskell. (Haskell does this in a particularly nice way...) --Joe English jeenglis@alcor.usc.edu