From 2179972753443407093
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 109fba,109af18a94b8a35c
X-Google-Attributes: gid109fba,public
X-Google-Thread: f78e5,e5cae9ffbf1dd57e
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 1992-10-05 13:59:46 PST
Xref: sparky comp.lang.c++:14547 comp.std.c++:1316
Path: sparky!uunet!snorkelwacker.mit.edu!ai-lab!life.ai.mit.edu!tmb
From: tmb@arolla.idiap.ch (Thomas M. Breuel)
Newsgroups: comp.lang.c++,comp.std.c++
Subject: Re: The concept of templates considered ill designed
Message-ID: <TMB.92Oct5200113@arolla.idiap.ch>
Date: 6 Oct 92 00:01:13 GMT
References: <Ienr=wm00awU8A=Ecc@andrew.cmu.edu> <23801@alice.att.com>
	<0enxJ1W00awTMNepU8@andrew.cmu.edu>
	<1992Oct5.160022.29224@genghis.borland.com>
Sender: news@ai.mit.edu
Reply-To: tmb@idiap.ch
Followup-To: comp.lang.c++
Organization: IDIAP (Institut Dalle Molle d'Intelligence Artificielle
	Perceptive)
Lines: 45
In-reply-to: pete@genghis.borland.com's message of 5 Oct 92 16:00:22 GMT

In article <1992Oct5.160022.29224@genghis.borland.com> pete@genghis.borland.com (Pete Becker) writes:

   [discussion of how to go about adding various forms of type constraints
   to template arguments deleted]

	   But this has very little to do with templates.  Constrained parameters
   and no further knowledge of the actual type is equivalent, as far as I can see,
   to simply carrying around a pointer to the actual class, and dealing with it
   only through the known interface.  You don't need templates to implement this,
   and I don't see that there's anything gained by using the template mechanism
   to do it.

That's not true. Unlike when you rely on inheritance, you can use a
template with any type that has the needed operations defined on it.
The type doesn't have to inherit from some common base type. The type
doesn't even have to be a class/struct.

For example, it is quite frequent that people write numerical template
code that works with an object that is vaguely number like. You'd like
to be able to use it with the built-in numerical types, as well as any
user defined types, as well as any types from a commercial library.
None of those types will inherit from one another, or from a common
"Number" type, nor is it entirely trivial to make this happen.

Another important reason is efficiency. Templates will generate code
that is specialized for the exact types that I'm using.

A third reason is that for templates, unlike for "dealing with classes
through a known interface", I can express type relationships between
arguments, and between arguments and return types.

	template <class A> A f(A x,A y) { ... }

is a very different function from

	A g(A x,A y) { ... }

even if all the objects you hand to "f" are subtypes of "A".

Allowing constraints on template arguments has proven to be very
useful in other languages. I think such a feature would be very
important in C++ as well. I would like to see both inheritance based
constraints and signature based constraints.

					Thomas.


