Topic: Templates and type checking


Author: tom@ssd.csd.harris.com (Tom Horsley)
Date: 24 Oct 91 18:04:52 GMT
Raw View
I don't expect this comment to have any effect on the standard at this
point, but I would really like to register my objections to templates as
defined in the ARM (which is apparently very similar if not identical to the
current state of the standard).

I would *much* rather explicitly instantiate templates that have them pop
into existence willy-nilly. It seems to me that having templates which are
automatically instantiated when you reference them is an open invitation to
loose all the type checking which has been one of the benefits of C++ over
C. The way templates are defined now, if I invoke a template function with
the "wrong" arguments, I don't get a compile time error, I just magically
get a new function (which may or may not be what I meant).

Another problem crops up with static members in template classes. Do I get
unique copies of the static members for each "unique" instantiation of the
template class? How unique does unique have to be? What if I want two
absolutely identical classes, but I want to have separate copies of their
static members (two different collections of integers, just as an example)?
For that matter, what name do I use to reference a static member of a
template class?

It seems to me that inventing some mechanism to explicitly declare an
instantiation of a template would not only solve the problems I talked about
here, but would probably remove the syntatic ambiguities with '<' and '>'
and would avoid the problems with figuring out where to actually generate
code for the instantiation (and how to avoid generating multiple copies of
the same instantiation).

Anyway, I am donning my flame-proof suit now. Like I said, I don't expect
templates to change, but I don't like them much either. (Just as a point of
reference, I seem to recall that Ada does require explicit instantiation of
generics, which are in some ways similar to templates).
--
======================================================================
domain: tahorsley@csd.harris.com       USMail: Tom Horsley
  uucp: ...!uunet!hcx1!tahorsley               511 Kingbird Circle
                                               Delray Beach, FL  33444
+==== Censorship is the only form of Obscenity ======================+
|     (Wait, I forgot government tobacco subsidies...)               |
+====================================================================+




Author: pete@borland.com (Pete Becker)
Date: 25 Oct 91 18:21:25 GMT
Raw View
In article <TOM.91Oct24130452@rcx1.ssd.csd.harris.com> tom@ssd.csd.harris.com (Tom Horsley) writes:
>
>Another problem crops up with static members in template classes. Do I get
>unique copies of the static members for each "unique" instantiation of the
>template class? How unique does unique have to be? What if I want two
>absolutely identical classes, but I want to have separate copies of their
>static members (two different collections of integers, just as an example)?
>For that matter, what name do I use to reference a static member of a
>template class?

 Two uses of a template refer to the same instantiation if they have
"identical" parameter lists.  The meaning of "identical" is discussed in the
ARM.  Each instantiation has its own static members:

        template <int i> class StaticData
 {
 static int counter;
 };

 template <int i> int StaticData<i>::counter = 0;


 typedef StaticData<1> Static1;
 typedef StaticData<2> Static2;
 typedef StaticData<3> Static3;

 This produces three "identical" classes with separate copies of the
static data.  The typedefs aren't necessary, but they make it possible to
use a different class in place of StaticData<1> etc. when that's appropriate.
 -- Pete