From: shang@corp.mot.com (David (Lujun) Shang)
Subject: Re: The concept of templates considered ill designed
Reply-To: shang@corp.mot.com
Organization: Motorola, Inc., Software Research and Development, Rolling Meadows, IL. 60008
Date: Tue, 6 Oct 92 00:29:28 GMT

In article <1992Oct2.153826.12051@ucc.su.OZ.AU> maxtal@extro.ucc.su.OZ.AU (John  
MAX Skaller) writes:
> 
> 	How would you modify the template idea to be better?
> 

Constrain class parameters. That's the way I can see to make the template 
idea better. For example:

   class Group < class MemberType > 
   // unconstrained class parameter MemberType here is equivalent
   // to MemberTyep[anyclass] 
   { public:
       virtual void accept ( MemberType m);
   };

   class AnimalGroup: public Group < class MemberType[Animal] >
   {  public:
        virtual void accept ( MemberType m)
        // Here the input interface of member function accept
        // is implicitly narrowed to a type of Animal
        {  Group::accept(m);
           // do something special on Animal here...
        };
   };

   class MammalGroup: public AnimalGroup < class MemberType[Mammal] >
   {  public:
        virtual void accept ( MemberType m)
        // Here the input interface of member function accept
        // is implicitly narrowed to a type of Mammal
        {  AnimalGroup::accept(m);
           // do something special on Mammal here...
        };
   };

   class BearGroup: public MammalGroup < class MemberType=Bear >
   {  public:
        virtual void accept ( MemberType m)
        // Here the input interface of member function accept
        // is implicitly narrowed and FIXED to Bear
        {  MammalGroup::accept(m);
           // do something special on Bear here...
        };
   };

or, simply,

   typedef MammalGroup <class MemberType=Bear> BearGroup;


> >Template is not a self-enclosed type-safe language feature, i.e. it cannot 
> >be compiled separately to ensure type-error free. All type errors about the 
> >class parameters are checked when the template is used. When the 
> >library-developers ship their products, they cannot ensure their templates 
> >are type-error free.  
> 
> 	This is not true, of course they can. What you mean
> is that the compiler doesn't help very much, and I agree.
> But I'm sure one could prove that a template was error free
> by hand ( I agree this is not formal system like a compiler check,
> but lets not get into whether a proof actually proves what it purports
> to or not: the usual solution to this problem is to publish the
> proof ).
>

What do you think false in my statement? I'm confused. Temaplate is safe 
at run time, no doubt. But it is not a self enclosed type-safe entity. 
What I mean is that template is not type-safe for separate compilation.
However, it is type-safe after linking. It is not the fault of the 
compiler or any implementation of the templates. The fault lies in 
template itself: its parameterized interface is not "typed" or 
"constrained".
  
> >The library user will get confused when an error is reported on the source  
code  
> >of the template: "should it be my fault or the fault of the template  
itself?"
Xref: sparky comp.lang.c++:14479 comp.sys.sun.apps:2148