Article View: comp.std.c++
Article #756Re: The concept of templates considered ill designed
From: David (Lujun) Shang
Date: Tue, 06 Oct 1992 02:00
Date: Tue, 06 Oct 1992 02:00
63 lines
2976 bytes
2976 bytes
In article <1992Oct5.160022.29224@genghis.borland.com> pete@genghis.borland.com
(Pete Becker) writes:
>
> 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.
Really? In my O-O design experience, it is very common to have a generic
model which is composed of several sub-models:
M < SM1, SM2, SM3 >
and then use the genric model to produce another generic model M':
M' < SM1', SM2', SM3' >
with more detailed specification. Sub-models in M' are usually covariantly
specified. As far as I can see, only constrained parameterized class can
support this concept. Sure, we can use pointers or even void* to implement
this concept, but it is never a serious programming style. To be honest, I
did not implement this concept in template (it's hard to figure out how can
template to support this), instead, I use pointers. But I have to use
expressions like:
((Derived*)->foo((DeriveInput*) input);
((SubDerived*)(((Derived*)->sub_model)->)->sub_foo();
((SubSubDerived*)(((SubDerived*)((((Derived*)->sub_model)->)->...;
...
How ugly they are!
> Templates avoid the extra indirection that's needed when you don't
> know what type you're dealing with.
It is an implementation issue. Eiffel uses pointers to access to members of
the type which is a parameter. There is an alternative way to implement
them without indirection. Indirection is required only when the polymorphism
is used. For example:
class BaseList < class EleType[Base], int size >
{ public: // make data member public for simplifying the example
EleType eles[size];
};
BaseList * bl;
BaseList<class EleType=Derived, size=10> dl;
bl = &dl;
bl->eles[2]; // dynamically computing the size of EleType is required
dl.eles[2]; // address is computed statically
Constrained parameterized classes does not necessarily have extra
indirection. Indirection is needed only when polymorphism based on
the parameterized classes is required. And such polymorphism is NOT
supported by C++ templates.
David Shang
Technical Details
Message-ID:
<1992Oct6.020042.11130@cadsun.corp.mot.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!panther!mothost!motsrd!newsReferences:
<1992Oct5.160022.29224@genghis.borland.com>Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c++,comp.lang.c++ Path: arc1-ca.pugleaf.net!sparky!uunet!panther!mothost!motsrd!news From: shang@corp.mot.com (David (Lujun) Shang) Subject: Re: The concept of templates considered ill designed Message-ID: <1992Oct6.020042.11130@cadsun.corp.mot.com> Sender: news@cadsun.corp.mot.com Reply-To: shang@corp.mot.com Organization: Motorola, Inc., Software Research and Development, Rolling Meadows, IL. 60008 References: <1992Oct5.160022.29224@genghis.borland.com> Date: Tue, 6 Oct 92 02:00:42 GMT Lines: 64