Topic: Member functions of class templates


Author: ziporama@aol.com (Ziporama)
Date: 1997/12/29
Raw View
Paul D. DeRocco writes:

>
>Luddy Harrison wrote:
>>
>> Section 14.5.1.1 of CD2 says this:
>>
>>    A member function of a class template is implicitly a member
>>    function template with the template-parameters of its class
>>    template as its template parameters.
>>
>> Could someone familiar with the intent behind this wording give me an
>> example of this rule at work?
>
>It's simpler than you think. All it means is that when you instantiate the
>class, you also instantiate the member function. Since there's a different
>version of the class for each combination of template parameters, there's
>also
>a different version of the member function, even though the declaration of
>the
>member function doesn't show the <...> notation.
>
>--
>
>Ciao,
>Paul
>---

Thanx Paul!
I am just a newbie, trying to figure it all out......you didn't just give me a
decent explanation.....you gave me another way of looking at code. And it
doesn't even matter if the explaination is correct or not! (although i suspect
it is, at least trivially)

>[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
>                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
>  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
>  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
>  Comments? mailto:std-c++-request@ncar.ucar.edu
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Pete Becker <petebecker@acm.org>
Date: 1997/12/15
Raw View
Stanley Friesen wrote:
>
> On 11 Dec 1997 18:21:56 PST, Luddy Harrison <luddy@concmp.com> wrote:
>
> >
> >Could someone familiar with the intent behind this wording give me an
> >example of this rule at work?  It seems to imply that if I write
> >
> >   template <class S> class T {
> >      public:
> >        int f(S*);
> >        int g();
> >   };
> >
> >that f is (implicitly) a function template, which I would interpret to
> >mean that more than one f can be instantiated from this declaration,
> >according to the argument types presented to f at a call.
>
> Yes.

Well, not really. There is no template function f(S*) dangling out
there. You can only name it in the context of some class T<S>, which is
what determines the type of S:

template <class S> class T {
   public:
     int f(S);
};
int main()
{
T<int> ti;
ti.f(3); // calls T<int>::f(int)
ti.f(3.0); // calls T<int>::f(int)
}

The purpose of this rule is simplify the description of how member
functions of template classes work. There is, however, a mechanism for
creating member function templates:

template <class S> class T {
   public:
     template<class U> int f(U);
};

Now the argument to f() determines what gets instantiated:

int main()
{
T<int> ti;
ti.f(3); // calls T<int>::f(int)
ti.f(3.0); // calls T<int>::f(double)
}
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Luddy Harrison <luddy@concmp.com>
Date: 1997/12/11
Raw View
Section 14.5.1.1 of CD2 says this:

   A member function of a class template is implicitly a member
   function template with the template-parameters of its class
   template as its template parameters.

Could someone familiar with the intent behind this wording give me an
example of this rule at work?  It seems to imply that if I write

   template <class S> class T {
      public:
  int f(S*);
  int g();
   };

that f is (implicitly) a function template, which I would interpret to
mean that more than one f can be instantiated from this declaration,
according to the argument types presented to f at a call.  But I can't
see how the usual mechanism for instantiating function templates can
come to play in the case of f.  It is not as though I can write
something like this:

class X { /* stuff */ };
class Y : public X { /* other stuff */ };

int T<Y>::g() {
   X* x;
   f(x);
}

and expect that class T<X> will be instantiated to give rise to
int T<X>::f(X*) (because T<X> is not a base class of T<Y>).

I get the feeling I'm missing the point of this clause.  Any
enlightenment would be appreciated.

Thanks.

-Luddy Harrison
_______________________________________________________________
Luddy Harrison                 Connected Components Corporation
email: luddy@concmp.com                      297 Newbury Street
phone: +1 617 262 8266                                 Suite 31
  fax: +1 617 262 8209                    Boston, MA 02115, USA
  www: http://www.concmp.com
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/12/13
Raw View
Luddy Harrison wrote:
>
> Section 14.5.1.1 of CD2 says this:
>
>    A member function of a class template is implicitly a member
>    function template with the template-parameters of its class
>    template as its template parameters.
>
> Could someone familiar with the intent behind this wording give me an
> example of this rule at work?

There is no practical application of this rule. In particular,
it doesn't mean that a member of a template class can't be
virtual (on other words it can be virtual), unlike a member
template.

--

Valentin Bonnard                mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: sfriesen@americasttv.com (Stanley Friesen)
Date: 1997/12/13
Raw View
On 11 Dec 1997 18:21:56 PST, Luddy Harrison <luddy@concmp.com> wrote:

>
>Could someone familiar with the intent behind this wording give me an
>example of this rule at work?  It seems to imply that if I write
>
>   template <class S> class T {
>      public:
>  int f(S*);
>  int g();
>   };
>
>that f is (implicitly) a function template, which I would interpret to
>mean that more than one f can be instantiated from this declaration,
>according to the argument types presented to f at a call.

Yes.

>  But I can't
>see how the usual mechanism for instantiating function templates can
>come to play in the case of f.

I think you mean g.

You are forgetting the implicit 'this' parameter.  Thus the effective
signature of g is: int g(S *this).

This means that the tpye of the object on which g is called determines
which g is used.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1997/12/13
Raw View
Luddy Harrison wrote:
>
> Section 14.5.1.1 of CD2 says this:
>
>    A member function of a class template is implicitly a member
>    function template with the template-parameters of its class
>    template as its template parameters.
>
> Could someone familiar with the intent behind this wording give me an
> example of this rule at work?

It's simpler than you think. All it means is that when you instantiate the
class, you also instantiate the member function. Since there's a different
version of the class for each combination of template parameters, there's also
a different version of the member function, even though the declaration of the
member function doesn't show the <...> notation.

--

Ciao,
Paul
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]