Topic: Random Walks Away From "The concept of templates considered ill designed


Author: pete@genghis.borland.com (Pete Becker)
Date: Thu, 8 Oct 1992 17:16:05 GMT
Raw View
In article <1992Oct7.155329.24107@cadsun.corp.mot.com> shang@corp.mot.com writes:
>Exactly. See, I call it a constrained parameterized class and never
>call it a template. In fact, a constrained parameterized class IS a
>class, it can be used anywhere as an abstract class is used. Therefore,
>it is NOT a template. For exmaple:
>
>  class < class MemberT[GroupMember] > Group {...};
>
>You can use Group to declare a pointer or a function argument:
>
>  Group * group;
>  group = new Group <class MemberT=SomeMemberType> (...);
>

 What does that get you that you can't do with the language as it now
stands?  Isn't this equivalent?

 class Forwarder
 {
     SomeMemberType *target;
 public:
     Forwarder( SomeMemberType *targ ) : target(targ) {}
     // member functions go here.  They simply invoke members
     // of SomeMemberType for target.
 };

 Forwarder *group;
 group = new Forwarder( new SomeMemberType );

 Now, granted, this is a little more work to set up.  But is there
anything that a "constrained parametrized class" can do that can't be done
this way?

>or,
>
>  void foo (Group & group);
>
>You cannot use template this way.
>

 No, but you can use inheritance this way.  Simply write your template
so that every instantiation inherits from a common base class:

 class Group
 {
 // whatever...
 };

 template <class T> class Derived : public Group
 {
 // whatever...
 };

 Derived<int> di;
 foo( di );

 -- Pete