[comp.archives] [c++] RE: Generics in C++

stenger@csc.ti.com (Dan Stenger) (04/22/91)

Archive-name: languages/c++/templates-cpp/1991-04-11
Archive: csc.ti.com:/pub/cpp.tar.Z [192.94.94.1]
Original-posting-by: stenger@csc.ti.com (Dan Stenger)
Original-subject: RE: Generics in C++
Reposted-by: emv@msen.com (Edward Vielmetti, MSEN)

In article <172@devnull.mpd.tandem.com> you write:
 > > In article <1410@h.cs.wvu.wvnet.edu> schiebel@cs.wvu.wvnet.edu
 > > >
 > > >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:
 > 
 > You got it!  There is a better way...
 > 
 > I pulled a PD version of a Decus (???) cpp that Texas Instruments had
 > extended to support templates a number of months ago -- I can't remember
 > the internet address.  (Mary, Dan -- you there?)

Here I am.  Following is the announcement that I sent out about this
preprocessor.  We continue to find it quite useful.  There is also a paper
describing this work in the "C++ at Work - 90 Conference Proceedings" (page
73).

	Dan Stenger
	Texas Instruments
	Computer Science Center
	stenger@csc.ti.com

------------------------------------------------------------------------------

To encourage additional experimentation with parameterized types in the C++
programming community we have made available an implementation of "templates"
as described by Stroustrup in his 1988 USENIX C++ Conference paper
"Parameterized types for C++".  Here is a simple example of this syntax:


template<class T> class Vector<T> {
	T* v;
	int sz;
  public:
	Vector<T>(int);
	T& operator[](int);
	inline T& elem(int i) { return v[i]; }
};

template<class T>
T& Vector<T>::operator[](int i) {
	if (i<0 || sz<=i) error("vector: range error");
	return elem(i);
};

template<class T>
Vector<T>::Vector<T>(size) {
	v = new(T[size]);
};

DECLARE Vector<char*>;   // create definitions for a vector of strings
IMPLEMENT Vector<char*>; // generate code to support vector of strings
Vector<char*> vs(30);	 // declare a vector of 30 strings


You should note that the syntax for templates is still evolving and the
current proposed standard is slightly different from what is implemented.

This implementation is provided through an enhanced macro facility in a
portable C++ preprocessor.  We started with the DECUS C preprocessor that is
provided in the MIT X Consortium distribution.  First, we made it compliant
with ANSI C (except for tri-graphs).  Next, we made it recognize a #pragma
form with the syntax "#pragma defmacro NAME PROGRAM", where NAME is the macro
keyword to use in the source text and PROGRAM is a program to execute to
translate the macro.  Finally, we wrote the defmacros to translate the
template syntax to standard C++.

Interested parties can find all of this available for anonymous ftp from:

	csc.ti.com	(internet address 128.247.159.141)

in compressed form in the file:

	/pub/cpp.tar.Z

Be forewarned that this is experimental, research software (although we have
used it extensively here at TI) and the standard syntax for parameterized
types is still evolving.  Also, I am willing to help and answer questions but
I only have a limited capacity to deal with them.

-- comp.archives file verification
csc.ti.com
-rw-r--r--  1 4        1          155912 Jul 20  1990 /pub/cpp.tar.Z
found templates-cpp ok
csc.ti.com:/pub/cpp.tar.Z