Topic: Templates


Author: Terence Kelling <kelling@arlut.utexas.edu>
Date: 1998/07/25
Raw View
Yes, member template functions are allowed, and the syntax is pretty much as
you described.  Specifically, it would be:

class A {

   public:
      template <class T> void func(T t);
};

Please note that you need to specify the function's return type and give an
identifier to the type being passed in.

To define the function outside of the class body, you would write:

template <class T> void A::func(T t) {

}

If class A is itself a template class such as:

template <class U> class A {

   public:
      template <class T> void func(T t);
}

then to define the function outside of the class body you would write:

template <class U> template <class T> void func(T t) {

}

Be aware that member templates can not be declared virtual and that there are
not many compilers that support them.  gcc and KAI's compiler's do and
Microsoft VC has some support for them if they are defined within the class
body.

Hope this helps,
Terence Kelling


Kannan Chellappan wrote:

> Hi all,
>
> I would like to know if it is possible to have a template member
> function ??
>
> i.e.
>
> class A {
> ...
> ...
>
> public:
>   template<class T> func(T ); // this function can take a parameter of
> any kind
> ...
> ...
> };
>
> Is this allowed and if so, its syntax pl. (i.e the syntax used to define
> it ).
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Terence Kelling <kelling@arlut.utexas.edu>
Date: 1998/07/29
Raw View
Jeff Rife wrote:

> What about the even more specific:
>
> template <class U>              // The parent template class
> template <class T>              // The func template
> void A<U>::func(T t) { ... }    // The func itself
>
> Which, if any, of these is correct WRT the standard?  If more than one
> is correct, which is preferred?

  Again, my haste led to confusion and errors.  The member function, if
declared outside the scope of the body, needs to be written as you have above.
I again apologize for the misunderstanding I caused.

Terence Kelling
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: David R Tribble <david.tribble@noSPAM.central.beasys.com>
Date: 1998/07/27
Raw View
Terence Kelling wrote:
> If class A is itself a template class such as:
>     template <class U> class A {
>        public:
>           template <class T> void func(T t);
>     }
>
> then to define the function outside of the class body you would write:
>     template <class U> template <class T> void func(T t) {
>     }

Shouldn't this instead be written (spread out for clarity):

    template <class U>          // The parent template class
    template <class T>          // The func template
    void A::func(T t) { ... }   // The func itself
         ^^^

In other words, how do I distinguish A::func() from another
func(), which might be a template member func of class B?

(Forgive me if my template syntax is wrong; I'm still learning
complicated declarations like this one.)

-- David R. Tribble, dtribble@technologist.com --
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/07/29
Raw View
Kannan Chellappan <chellapk@nmasd.bel.alcatel.be> writes:

> I would like to know if it is possible to have a template member
> function ??

Yes, it is.

> class A { public: template<class T> func(T ); };

> Is this allowed

Yes, except that you must specify a return type for any function, even
for template member functions. :-)

--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Terence Kelling <kelling@arlut.utexas.edu>
Date: 1998/07/29
Raw View
David R Tribble wrote:

> Shouldn't this instead be written (spread out for clarity):
>
>     template <class U>          // The parent template class
>     template <class T>          // The func template
>     void A::func(T t) { ... }   // The func itself
>          ^^^

Yes, you are absolutely correct.  I was just trying to type too fast and left
the "A::" off.  I apologize for any confusion this may have caused.

Terence Kelling
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Jeff Rife <jrife@nabs.net>
Date: 1998/07/29
Raw View
David R Tribble wrote:
> Terence Kelling wrote:
> >     template <class U> template <class T> void func(T t) {
> >     }
>
> Shouldn't this instead be written (spread out for clarity):
>
>     template <class U>          // The parent template class
>     template <class T>          // The func template
>     void A::func(T t) { ... }   // The func itself
>          ^^^
>
> In other words, how do I distinguish A::func() from another
> func(), which might be a template member func of class B?

What about the even more specific:

template <class U>              // The parent template class
template <class T>              // The func template
void A<U>::func(T t) { ... }    // The func itself

Which, if any, of these is correct WRT the standard?  If more than one
is correct, which is preferred?

--
Jeff Rife                   | "Because he was human; because he had
goodness;
19445 Saint Johnsbury Lane  |  because he was moral they called him insane.
Germantown, MD  20876-1610  |  Delusions of grandeur; visions of splendor;
Home: 301-916-8131          |  A manic-depressive, he walks in the rain.
Work: 301-770-5800 Ext 5335 |              -- Rush, "Cinderella Man"
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Kannan Chellappan <chellapk@nmasd.bel.alcatel.be>
Date: 1998/07/23
Raw View
Hi all,

I would like to know if it is possible to have a template member
function ??

i.e.

class A {
...
...

public:
  template<class T> func(T ); // this function can take a parameter of
any kind
...
...
};

Is this allowed and if so, its syntax pl. (i.e the syntax used to define
it ).



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Steven Kefford" <s.kefford@mia.co.uk>
Date: 1998/07/24
Raw View
Kannan Chellappan wrote in message
<35B720A6.FC7A6699@nmasd.bel.alcatel.be>...
>Hi all,
>
>I would like to know if it is possible to have a template member
>function ??
>
>i.e.
>
>class A {
>...
>...
>
>public:
>  template<class T> func(T ); // this function can take a parameter of
>any kind
>...
>...
>};
This should be possible, but I would like to know if there are any compilers
out there that support this yet?

-----------------------------------------------------------
Steven Kefford
(+44) (0)1438 739669
s.kefford@mia.co.uk*remove-this*




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "S.N.Pappu" <spappu@hss.hns.com>
Date: 1998/07/08
Raw View
I would like to know the meaning of passing derived class template as
argument to base class template.

*******************************************************************************
 Others minds are wretched places to be the source of our happiness
                                                -Schopenhauer
******************************************************************************



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Jorn Ronnow" <d97jorn.remove_this@dtek.thisgoesaway.chalmers.se>
Date: 1998/07/08
Raw View
S.N.Pappu wrote in message ...
>I would like to know the meaning of passing derived class template as
>argument to base class template.


Here's an example:

template <class T> class base
{
  public:
    void foo(T *bar);
};

class derived : public base<derived>
{
  // ...
};

--Jorn Ronnow



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: jjb@watson.ibm.com (John Barton)
Date: 1995/06/12
Raw View
In article <MATT.95Jun7152344@godzilla.EECS.Berkeley.EDU>, matt@godzilla.EECS.Berkeley.EDU writes:
|> In article <D9tLJE.CCL@dorite.use.com> juggler@dorite.use.com (Phil Paxton) writes:
|>
|> > >The IBM CSet++ seems to have very good template implementation. It is
|> > >available under OS/2 and AIX. I wish it was available on more platforms..
|> >
|> > I thought it has been said (in various magazines) [that] IBM has abandoned
|> > CSet and instead is pushing their Visual Age C++ ?
|>
|> Same product, new name.  I have a CD-ROM that has "C Set++ 3.0 beta"
|> stamped on the disk itself, and a little sticker on the jewel box
|> saying "The new name for this product is VisualAge C++, version 3.0".
|>
|> Whatever the name, I do agree that it's a nice product.  It doesn't
|> have namespaces, RTTI, mutable, explicit, and some of that other
|> recent stuff (at least beta II, which is the most recent version I've
|> seen, doesn't), but it's a very solid implementation of ARM-level C++.
|> --
|> Matt Austern          matt@physics.berkeley.edu
|> http://dogbert.lbl.gov/~matt

  Matt is correct, though maybe "next version of product, new name" would
be better. The core compiler in VisualAge C++ is v3.1 of the xlC++ compiler.
In the C Set ++ version IBM added more stuff than just a compiler, hence "Set".
In VisualAge C++, the visual builder called VisualAge, adapted to emit
C++ rather than SmallTalk, has been added.  Also available on Solaris too.

--
John.

John J. Barton        jjb@watson.ibm.com            (914)784-6645
 <http://www.research.ibm.com/xw-SoftwareTechnology>
H1-C13 IBM Watson Research Center P.O. Box 704 Hawthorne NY 10598





Author: neswold@cns38.fnal.gov (Richard Neswold)
Date: 1995/06/07
Raw View
In article <3r2f56$s6e@hermes.is.co.za>, datrix@apollo.is.co.za (Datrix Solutions) writes...
>Markus Schutz (schutz@patpserv.epfl.ch) wrote:
>: I don't know what's going on with template standardisation. But the
>: first time I saw a compiler handling templates was about 4 years ago.
>: I think in that time, most of the problems should have been solved.
>: I realy don't understand why it is so difficult to find a compiler
>: that accepts our code (that is realy written standard according to
>: various C++ programming books).
>
>The IBM CSet++ seems to have very good template implementation. It is
>available under OS/2 and AIX. I wish it was available on more platforms..
>
>cheers,
>Wayne

Watcom's C++ compiler supports everything except RTTI and namespaces. It *is*
available on more platforms :-) (OS/2, DOS, DOS 32-bit, Windows, Window NT,
AutoCAD ADS, Novel NLMs (?) and QNX).

Oops. I should change that to "more Intel-based platforms".

A happy Watcom customer,
-Rich





Author: juggler@dorite.use.com (Phil Paxton)
Date: 1995/06/07
Raw View
datrix@apollo.is.co.za (Datrix Solutions) writes:

>The IBM CSet++ seems to have very good template implementation. It is
>available under OS/2 and AIX. I wish it was available on more platforms..

I thought it has been said (in various magazines) [that] IBM has abandoned
CSet and instead is pushing their Visual Age C++ ?


--

 ------------------------------------
 Phil Paxton :: Fishers, Indiana, USA





Author: matt@godzilla.EECS.Berkeley.EDU
Date: 1995/06/07
Raw View
In article <D9tLJE.CCL@dorite.use.com> juggler@dorite.use.com (Phil Paxton) writes:

> >The IBM CSet++ seems to have very good template implementation. It is
> >available under OS/2 and AIX. I wish it was available on more platforms..
>
> I thought it has been said (in various magazines) [that] IBM has abandoned
> CSet and instead is pushing their Visual Age C++ ?

Same product, new name.  I have a CD-ROM that has "C Set++ 3.0 beta"
stamped on the disk itself, and a little sticker on the jewel box
saying "The new name for this product is VisualAge C++, version 3.0".

Whatever the name, I do agree that it's a nice product.  It doesn't
have namespaces, RTTI, mutable, explicit, and some of that other
recent stuff (at least beta II, which is the most recent version I've
seen, doesn't), but it's a very solid implementation of ARM-level C++.
--
Matt Austern          matt@physics.berkeley.edu
http://dogbert.lbl.gov/~matt





Author: datrix@apollo.is.co.za (Datrix Solutions)
Date: 1995/06/06
Raw View
Markus Schutz (schutz@patpserv.epfl.ch) wrote:
: In my lab, we currently develop a new working platform based on C++.
: The choice for C++ was done for several reasons, too long to enumerate

: The further we go with the development of this platform the more
: problems we have with most of the proposed compilers. Actually only
: g++ is able to compile the code and produce more less executable
: code. We saw a lot of errors either in the compilers, going from
: 'SIGNAL 11' trough 'internal error, segmentation fault' to
: 'sorry, not implemented', either in the produced program(s), going
: from strange program crashes to unexecutable code.

...
: I don't know what's going on with template standardisation. But the
: first time I saw a compiler handling templates was about 4 years ago.
: I think in that time, most of the problems should have been solved.
: I realy don't understand why it is so difficult to find a compiler
: that accepts our code (that is realy written standard according to
: various C++ programming books).

The IBM CSet++ seems to have very good template implementation. It is
available under OS/2 and AIX. I wish it was available on more platforms..

cheers,
Wayne





Author: schutz@patpserv.epfl.ch (Markus Schutz)
Date: 1995/05/30
Raw View
In my lab, we currently develop a new working platform based on C++.
The choice for C++ was done for several reasons, too long to enumerate
here. We are working in image and sequence coding, thus needing some
home made stuff concerning images and coding.

We begun with defining some basic classes like vectors and matrixes,
in order to get some image classes. As we need to be as general as
possible in the definition of the basic classes, the "library" is
constructed on templates. We would be able to construct matrixes of
various element types for example.

As we havent found a compiler/linker that is able to handle templated
stuff in an easy way, the code for the basic classes is always
recompiled. All is put in included files containing class definition
and the implementation. To be able to build modules linked with a
main program, all the implementation stuff is done in inline mode,
so we avoid problems at link time (like double defined names).

I know the code produced like that becomes huge (because of all the
inlines) and will probably not be very efficient. But we do not aim
to have optimal code. We only want a working platform that is easy
to work with. Like having for an object the data and properties linked
together, in order to avoid a lot of debuging problems we had in the
past with C writen code.

The further we go with the development of this platform the more
problems we have with most of the proposed compilers. Actually only
g++ is able to compile the code and produce more less executable
code. We saw a lot of errors either in the compilers, going from
'SIGNAL 11' trough 'internal error, segmentation fault' to
'sorry, not implemented', either in the produced program(s), going
from strange program crashes to unexecutable code.

I don't know what's going on with template standardisation. But the
first time I saw a compiler handling templates was about 4 years ago.
I think in that time, most of the problems should have been solved.
I realy don't understand why it is so difficult to find a compiler
that accepts our code (that is realy written standard according to
various C++ programming books).

So if you know the solution to our problems, namely if you know a
realy good C++ compiler that can handle template based code with
a lot of inlined functions, that does not produce "crash code" when
you use virtual functions and that runs under UNIX, I would be glad
to hear about.

For people constructing compilers, we would be realy happy if you
could solve the template problem. For example, why isn't it possible
to build a real library (with object or near object code) with
templates based classes ? Why no making linkers smarter (or something
like a pre-linker) that could handle templates ? This would realy
help. It would also realy help if we could get a compiler that doesn't
tell us 137 times a day that there is an internal error. We would like
to have working compilers (at least one).

If you know solutions to parts of our problems, I'll be glad to hear
from you. Thanks in advance.

Markus

PS: We have seen a lot of platforms with vectors, matrixes and various
    other more less usable stuff. Even complete image classes. But none
    of them were usable for the requirements we have.





Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Sat, 28 May 1994 19:00:26 GMT
Raw View
Some ideas to "finish off" templates.

------------------------------------------------------------------
            ISO:       WG21/N0xxx
            ANSI:      94-xxxx
            Author:    John Max Skaller
            Date:      May 1994
            Reply to:  maxtal@suphys.physics.su.oz.au

"COMPLETION OF TEMPLATE FACILITIES - SUMMARY"
---------------------------------------------

The following items are possible additions to the
template facilities. The list below is intended as a guide,
not a full specification.

1) Namespace Templates.
-----------------------

Example:

  template<class T> namespace X { int x; int f() { return x; } }
  X<int>::x = 1;
  using namespace X<int>;
  cout << f();

  namespace X<int> { void g() {} }
  X<int>::g();

Semantics:

  A namespace template is similar to a class template with only
  static members. It differs in being extensible.


Uses:

  An extensible set of related functions, types and variables can be
  defined or declared in a namespace and parameterised
  using templates. Unlike classes, these do not represent an object.

Restrictions:

  None special to namespace templates.

2) Typedef Templates
--------------------


Example:

  template<class T> typedef X<T> Y<Y>; // make alias

  template<class T, class U> class map { .. }
  template<class T> typedef map<int, T> array<T>; // partial closure


  template<class T, template<class T> class set> class container { ..}
  container<float,array> x; // typedef required for passing templates

Semantics:

  A family of typedefs.

Uses:

  This is an important facility, it is necessary for reordering
  and binding a subset of the formal parameters,
  to produce a form required for passing a template to a template.

  Required for creating an alias for a template.

  Generally useful for aliasing subsets of generic families.

Restrictions:

  Each template formal must appear in both the LHS and RHS of
  the template typedef.


3) Template templates
---------------------

Example:

  template<class T> template<class U> class X;
  X<int><long> xil;

Semantics:

  Equivalent to a single template with concatented formal
  parameter lists.

Uses:

  Stylistic.

4) friend templates
-------------------

Example:

   template<class T>
   class X {


     template<class U> friend class A<U>;  //FRIEND TEMPLATE
     // A<U> is a friend of X<T> for all T and U
     // MANY TO MANY

     friend class B; // single class B is a friend of each X<T>
     // MANY TO ONE

     friend class C<T>; // single class C<T> is a friend of X<T>
     // ONE TO ONE
   }

Semantics:

  Declare a family of friends of a class.

  Note that a friend declaration in a class template
  declares a single friend. Only members are parameterised
  along with the class.


Comment: Loss of encapsualtion is possible in combination
with specialisations, however, the programmer has
explicitly granted access to a family, which is
sometimes necessary. For example:

  template<class T>
  class IntNumber {
    T value;
    template<class U> friend class FloatNumber<U>;
    template<class U> friend class IntNumber;
  public:
    template<class U> void addFloat(const FloatNumber<U>&);
    //member template
  };

  template<class U>
  class FloatNumber {
    T value;
    template<class U> friend class IntNumber<U>;
    template<class U> friend class FloatNumber; // friends
  public:
    template<class U> void addInt(const IntNumber<U>&);
    //member template
  };

All the Int and Float Number classes are friends of each other.
This may be necessary to define pairwise specialisations
of members, in this case the add methods (which are themselves
member templates).


--
        JOHN (MAX) SKALLER,         INTERNET:maxtal@suphys.physics.su.oz.au
 Maxtal Pty Ltd,      CSERVE:10236.1703
        6 MacKay St ASHFIELD,     Mem: SA IT/9/22,SC22/WG21
        NSW 2131, AUSTRALIA




Author: David@f1.n100.z60.wlink.nl (David)
Date: Fri, 09 Oct 1992 18:43:01 -0100
Raw View
Organization: MAXTAL P/L C/- University Computing Centre, Sydney

I'm posting this for David Shang, whose News Server is down:

In article <23835@alice.att.com> ark@alice.att.com (Andrew Koenig)
writes:
> In article <1992Oct7.150501.23910@cadsun.corp.mot.com>
shang@corp.mot.com

writes:
>

> > I soppose f is defined as:
>
> >      <class T[Foo]> void f(T t) { t.mem(0); };
>
> > where keyword template is not necessary, because "f" can be  called
> > directly without any instantiation.
>
> Then why not just write
>
>       void f(T& t) { t.mem(0); }
>
> ?  Why bother with templates at all if that's what you want?
>

This is NOT the example I gave originally. I don't think template
has a big deal for this case either. It proves that template is
unnecessary for this example, not that contrained parameter is
unnecessary. Why bother with templates to play the trick of the
name?? Why cann't we use virtual functions?

> > I suppose "mem" defined in Foo is a virtual function, otherwise
"t.mem(0)"
> > will always call the "mem" defined in Foo.
>

> No; if it's virtual then you don't need templates.  The purpose of
> templates is to allow compile-time polymorphism between types that
> have members with the same names.
>

I don't think so. If the purpose of template is to allow compile-time
polymorphism between types that have members with the same name, why
bother with templates? Just for run-time speed? No. Dynamic binding
is not a significant overhead in C++.

Parameterization is used for a structure in which some component's
type is unknown or patially known at the time the paramerterized
feature is written. For example,

   class Vector < class MemberT, int size >
   {  MemberT members[size];
     public:
      MemberT & operator[] (int index) { return members[index]; };
      ...
   };

As MemberT is unknown to Vector, we SHOULD NOT assume any members  with
some name defined in MemberT. If MemberT becomes partially known in a
derived Vector class, we should constrain it:

    class NumVector: public Vector < class MemberT[Number] >
    { public:
       thisclass& operator+ (thisclass & other)
       { for (int i=0; i<size; i++) members[i] += other[i];
         return *this;
       };
       ...
    };

where += may be a virtual operator defined in Number.

If somebody says that this can be achieved without parameterization,
PLEASE GIVE me an example. The following example is what I can think
about:

   class Vector
   {  int    size;     // unnecessary overhead in object code
      int    ele_size; // unnecessary overhead in object code
      void * members;  // unnecessary indirection
     public:
      void * operator[] ... // untyped output
   };
   class NumVector: public Vector
   { public:
      NumVector& operator+ (NumVector & other)
      {  for (i=...)
             ((Number*)members[i]) += ((Number*)other[i]);
             // unsafe downcast here!!
         ...
      };
   };

Remember the code I give? I don't think you'll like it:

   ((DDDD*)(((DDD*)(((DD*)(((D*)d)->dd)))->ddd)->dddd)
   ->func(((DDDD*)(((DDD*)(((DD*)(((D*)d)->dd)))->ddd)->dddd)->data);

> > Not necessarily. Constrained parameterized function is not a  template
> > function. It does not necessarily act exactly like a template,
just as a
> > template function does not necessarily act exactly like a macro
> > definition.
>

> If you're proposing yet another brand new language mechanism, you  had
> better describe it in detail.  It isn't enough to say `it's just  like
> templates except for type constraints' because that's just not  true.
> --

>                               --Andrew Koenig
>                                 ark@europa.att.com


I didn't say constrained parameterized class "is just like template
except for type constrains". That's not true -- you are correct. A
complete description of constrained parameterized class is not a
simple
work, however. At least it is not so simple that can be clarified in
a few newsgroup posts.

David Shang


References: <1992Oct8.035346.2860@ucc.su.OZ.AU>
Reply-To: shang@corp.mot.com

In article <1992Oct8.035346.2860@ucc.su.OZ.AU>
maxtal@extro.ucc.su.OZ.AU (John

MAX Skaller) writes:
>

>       We already have abstract classes that are precisely what
> you want.


I don't think the current abstract classes can offer

what I want.

> A generic class even with constraints CANNOT generate
> actual code until instantiated.

Not true. A language named Cluster does generate actual codes
for constrained parameterized classes and functions. Does Effeil?
I'm not sure. But it seems possible, thought its implementation
may not.

> But it CAN be type checked at compile time.
>


Yes. But this is not the only purpose of a constrained parameterized

class.

>       Restricting a generic parater to a Base class is pointless.
> It only allows you to generate actual code in precisely those
> cases where you didn't need a template in the first place.
>

Really? Read my response to article <23835@alice.att.com>
(ark@alice.att.com), and give me an example to prove that

parameterization is not necessary for the example I gave.


David Shang


--
;----------------------------------------------------------------------
        JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
 Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------





Author: shang@corp.mot.com (David (Lujun) Shang)
Date: 9 Oct 92 16:43:01 GMT
Raw View
I'm posting this for David Shang, whose News Server is down:

In article <23835@alice.att.com> ark@alice.att.com (Andrew Koenig)
writes:
> In article <1992Oct7.150501.23910@cadsun.corp.mot.com>
shang@corp.mot.com

writes:
>

> > I soppose f is defined as:
>
> >      <class T[Foo]> void f(T t) { t.mem(0); };
>
> > where keyword template is not necessary, because "f" can be  called
> > directly without any instantiation.
>
> Then why not just write
>
>       void f(T& t) { t.mem(0); }
>
> ?  Why bother with templates at all if that's what you want?
>

This is NOT the example I gave originally. I don't think template
has a big deal for this case either. It proves that template is
unnecessary for this example, not that contrained parameter is
unnecessary. Why bother with templates to play the trick of the
name?? Why cann't we use virtual functions?

> > I suppose "mem" defined in Foo is a virtual function, otherwise
"t.mem(0)"
> > will always call the "mem" defined in Foo.
>

> No; if it's virtual then you don't need templates.  The purpose of
> templates is to allow compile-time polymorphism between types that
> have members with the same names.
>

I don't think so. If the purpose of template is to allow compile-time
polymorphism between types that have members with the same name, why
bother with templates? Just for run-time speed? No. Dynamic binding
is not a significant overhead in C++.

Parameterization is used for a structure in which some component's
type is unknown or patially known at the time the paramerterized
feature is written. For example,

   class Vector < class MemberT, int size >
   {  MemberT members[size];
     public:
      MemberT & operator[] (int index) { return members[index]; };
      ...
   };

As MemberT is unknown to Vector, we SHOULD NOT assume any members  with
some name defined in MemberT. If MemberT becomes partially known in a
derived Vector class, we should constrain it:

    class NumVector: public Vector < class MemberT[Number] >
    { public:
       thisclass& operator+ (thisclass & other)
       { for (int i=0; i<size; i++) members[i] += other[i];
         return *this;
       };
       ...
    };

where += may be a virtual operator defined in Number.

If somebody says that this can be achieved without parameterization,
PLEASE GIVE me an example. The following example is what I can think
about:

   class Vector
   {  int    size;     // unnecessary overhead in object code
      int    ele_size; // unnecessary overhead in object code
      void * members;  // unnecessary indirection
     public:
      void * operator[] ... // untyped output
   };
   class NumVector: public Vector
   { public:
      NumVector& operator+ (NumVector & other)
      {  for (i=...)
             ((Number*)members[i]) += ((Number*)other[i]);
             // unsafe downcast here!!
         ...
      };
   };

Remember the code I give? I don't think you'll like it:

   ((DDDD*)(((DDD*)(((DD*)(((D*)d)->dd)))->ddd)->dddd)
   ->func(((DDDD*)(((DDD*)(((DD*)(((D*)d)->dd)))->ddd)->dddd)->data);

> > Not necessarily. Constrained parameterized function is not a  template
> > function. It does not necessarily act exactly like a template,
just as a
> > template function does not necessarily act exactly like a macro
> > definition.
>

> If you're proposing yet another brand new language mechanism, you  had
> better describe it in detail.  It isn't enough to say `it's just  like
> templates except for type constraints' because that's just not  true.
> --

>                               --Andrew Koenig
>                                 ark@europa.att.com


I didn't say constrained parameterized class "is just like template
except for type constrains". That's not true -- you are correct. A
complete description of constrained parameterized class is not a
simple
work, however. At least it is not so simple that can be clarified in
a few newsgroup posts.

David Shang


From: shang@corp.mot.com (David (Lujun) Shang)
Newsgroups: comp.lang.c++,comp.std.c++
Subject: Re: The concept of templates considered ill designed
References: <1992Oct8.035346.2860@ucc.su.OZ.AU>
Reply-To: shang@corp.mot.com

In article <1992Oct8.035346.2860@ucc.su.OZ.AU>
maxtal@extro.ucc.su.OZ.AU (John

MAX Skaller) writes:
>

>       We already have abstract classes that are precisely what
> you want.


I don't think the current abstract classes can offer

what I want.

> A generic class even with constraints CANNOT generate
> actual code until instantiated.

Not true. A language named Cluster does generate actual codes
for constrained parameterized classes and functions. Does Effeil?
I'm not sure. But it seems possible, thought its implementation
may not.

> But it CAN be type checked at compile time.
>


Yes. But this is not the only purpose of a constrained parameterized

class.

>       Restricting a generic parater to a Base class is pointless.
> It only allows you to generate actual code in precisely those
> cases where you didn't need a template in the first place.
>

Really? Read my response to article <23835@alice.att.com>
(ark@alice.att.com), and give me an example to prove that

parameterization is not necessary for the example I gave.


David Shang


--
;----------------------------------------------------------------------
        JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
 Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------