Topic: Template member function
Author: phalpern@truffle.ma.ultranet.com (Pablo Halpern)
Date: 1996/08/22 Raw View
>>>>>> "AB" == Andrew Bell <abell@mindspring.com> writes:
>[...]
>AB> Also, while I'm on the subject of templates, I noticed in the DWP that
>AB> while you can (pardon my probably incorrect terminology) specialize a
>AB> templated class for a particular type, if you do so you must define
>AB> every function you want to have in that instantiation. Why did they
>AB> specify that? It seems like there would be times where I would just
>AB> want to specify a special version of a particular member function, and
>AB> this means there's a lot of work for me to go through. Is there a
>AB> backdoor way of doing this?
>
It is not necessary to speciallize the *class* in order to specialize a
single function of the class. Note the distinction:
template <class T>
class foo
{
void bar();
void baz();
};
// Specialize just the bar function for ints (Note, no template header)
void foo<int>::bar() { }
// Specialize the whole class for void* (I might have the syntax wrong)
template <class T>
class foo<void*>
{
// ...
};
Now you have to write a body for every function in foo<void*> that is
called in the program.
-------------------------------------------------------------
Pablo Halpern phalpern@truffle.ultranet.com
I am self-employed. Therefore, my opinions *do* represent
those of my employer.
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: abell@mindspring.com (Andrew Bell)
Date: 1996/08/09 Raw View
David Vandevoorde <vandevod@cs.rpi.edu> wrote:
[Other stuff you've seen already]
>In this explicit destructor call, the template keyword is neither
>required not allowed. I.e., this `template' prefix _only_ applies
>to names of _member_ templates with explicit argument dependency.
How do you specify a particular instance of a template function?
I've been working with the STL, and I had a vector of a templated
class that I wanted to sort, using a particular sort function. So I
wanted to do something like:
template <class NumericType> class Point<NumericType> {...}
template <class NumType> bool
Compare(const Point<NumType> &point, ...) {}
template <class NumType>void
foo()
{
vector<Point<NumType>> pointVector;
[...]
// Need to tell it which Compare to use!
sort(pointVector.begin(),pointVector.end(),Compare);
[...]
}
I ended up working around it, first by overloading operator< for the
Point<> class, and then in a better way by making Compare() a static
member function of the point class and doing
sort(...,Point<NumType>::Compare);
However, this requires me either to modify the Point<> class every
time I want a new comparator, or make up a bogus class with just that
function in the class. Is there a way to specify a specific template
function pointer?
Also, while I'm on the subject of templates, I noticed in the DWP that
while you can (pardon my probably incorrect terminology) specialize a
templated class for a particular type, if you do so you must define
every function you want to have in that instantiation. Why did they
specify that? It seems like there would be times where I would just
want to specify a special version of a particular member function, and
this means there's a lot of work for me to go through. Is there a
backdoor way of doing this?
Andrew Bell
abell@mindspring.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: vandevod@cs.rpi.edu (David Vandevoorde)
Date: 1996/08/12 Raw View
>>>>> "AB" == Andrew Bell <abell@mindspring.com> writes:
[...]
AB> How do you specify a particular instance of a template function?
AB> I've been working with the STL, and I had a vector of a templated
AB> class that I wanted to sort, using a particular sort function. So I
AB> wanted to do something like:
AB> template <class NumericType> class Point<NumericType> {...}
AB> template <class NumType> bool
AB> Compare(const Point<NumType> &point, ...) {}
AB> template <class NumType>void
AB> foo()
AB> {
AB> vector<Point<NumType>> pointVector;
AB> [...]
AB> // Need to tell it which Compare to use!
AB> sort(pointVector.begin(),pointVector.end(),Compare);
AB> [...]
AB> }
In principle (i.e., according to the DWP) you should be able to write:
sort(pointVector.begin(), pointVector.end(), Compare<NumType>);
However, few compilers support this yet. The compiler I use allows:
typedef bool (*Ordering)(Point<NumType> const&,
Point<NumType> const&);
Ordering cmp = Compare; // Template argument deduction
sort(pointVector.begin(), pointVector.end(), cmp);
I.e., the NumType argument is deduced from the function-pointer type.
[...]
AB> Also, while I'm on the subject of templates, I noticed in the DWP that
AB> while you can (pardon my probably incorrect terminology) specialize a
AB> templated class for a particular type, if you do so you must define
AB> every function you want to have in that instantiation. Why did they
AB> specify that? It seems like there would be times where I would just
AB> want to specify a special version of a particular member function, and
AB> this means there's a lot of work for me to go through. Is there a
AB> backdoor way of doing this?
You might be able to rely on inheritance (careful with overloading rules
though).
Daveed
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Carsten Arnholm <ca@sesam.dnv.no>
Date: 1996/08/06 Raw View
[Moderator's note: this article is crossposted to comp.std.c++ and
comp.lang.c++. Followups have been directed by default to comp.std.c++.
mha]
marek@iiasa.ac.at (Marek MAKOWSKI) wrote:
>Is it possible to have a template member function in a non-template class ?
>Something like:
>
>class A {
>public:
> A();
> // ....
>private:
> template <class T> process();
> // ....
>};
Sorry, this is not allowed (yet).
Refer to the book "The Design and Evolution of C++", by Bjarne Stroustrup.
In section 15.9.1 "Inheritance Relationships", (page 361) he talks about
smart pointers and how he would like them to have similar inheritance
behaviour as ordinary pointers. On the bottom of te page he says:
"Member templates - which are not currently part of C++ - provide a solution:
template<class T1> class Ptr { // pointer to T1
// ...
template<class T2> operator Ptr<T2>();
};"
This is basically the same as what you are asking for. I guess it will
become legal C++ at some stage, but currently we will have to do
without it.
BTW, your example will perhaps NEVER be legal, because your process()
member function template has no arguments. How do you think the compiler
is supposed to figure out which template function you want ?
Carsten Arnholm
ca@dnv.no
---
[ 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: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763)
Date: 1996/08/06 Raw View
[Moderator's note: this article is crossposted to comp.std.c++ and
comp.lang.c++. Followups have been directed by default to comp.std.c++.
mha]
In article <1996Aug6.061314.5177@iiasa.ac.at> marek@iiasa.ac.at (Marek
MAKOWSKI) writes:
|> Is it possible to have a template member function in a non-template class ?
According to the latest drafts, yes. This is a recent extension,
though, and not supported by most compilers.
Also, there are limits on the types of things that can be member
templates. I've not studied the list in detail, but I do remember that
non-virtual functions are OK, virtual functions no.
|> Something like:
|> class A {
|> public:
|> A();
|> // ....
|> private:
|> template <class T> process();
|> // ....
|> };
|> Both SunPro and Watcom give errors for such a code.
|> I can't define A<T> because T is decided upon after A is constructed.
I know for a fact that the Sun compilers do not support this. (I rather
suspect that they are waiting some stability in order to introduce all
of the changes at once, so we only have to change our code once, and not
with each release.)
I'm also pretty sure that the syntax is false. I'm not even sure what
you are trying to do. I presume that you want process to be a set of
functions that in some way depends on the type T. In this case, the T
must appear somewhere in the parameter list of the function, in order
that the compiler can know which version to generate. (I think that the
syntax here would be exactly the same as that of a non-member template
function.)
--
James Kanze Tel.: (+33) 88 14 49 00 email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils, tudes et r alisations en logiciel orient objet --
-- A la recherche d'une activit dans une region francophone
---
[ 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: vandevod@cs.rpi.edu (David Vandevoorde)
Date: 1996/08/06 Raw View
>>>>> "MM" == Marek MAKOWSKI <marek@iiasa.ac.at> writes:
MM> Is it possible to have a template member function in a non-template class ?
[...]
It is possible according to the current draft.
The only compilers I know who can handle this at this time, are the
ones based on EDG's front-end version 2.32.
Daveed
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: David Vandevoorde <vandevod@cs.rpi.edu>
Date: 1996/08/06 Raw View
Carsten Arnholm wrote:
[...]
> >class A {
> >public:
> > A();
> > // ....
> >private:
> > template <class T> process();
> > // ....
> >};
>
> Sorry, this is not allowed (yet).
> Refer to the book "The Design and Evolution of C++", by Bjarne
> Stroustrup.
D&E is not up to date in this respect. It is allowed now (at least
since CD1).
[...]
> BTW, your example will perhaps NEVER be legal, because your process()
> member function template has no arguments. How do you think the
> compiler
> is supposed to figure out which template function you want ?
By using the explicit syntax:
A a;
a.template process<int>();
Daveed
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/08/06 Raw View
marek@iiasa.ac.at (Marek MAKOWSKI) writes:
>Is it possible to have a template member function in a non-template class ?
Yes, so long as the member function isn't virtual.
However, this is a new feature, and most compilers don't support this yet.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: ajb@panix.com (Atman Binstock)
Date: 1996/08/07 Raw View
David Vandevoorde <vandevod@cs.rpi.edu> wrote:
>Carsten Arnholm wrote:
>[...]
>> >class A {
>> >public:
>> > A();
>> > // ....
>> >private:
>> > template <class T> process();
>> > // ....
>> >};
>By using the explicit syntax:
> A a;
> a.template process<int>();
Do you have to use the template key word?
I thought
a.process<int>();
was the way you called it.
Atman Binstock ajb@panix.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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: David Vandevoorde <vandevod@cs.rpi.edu>
Date: 1996/08/08 Raw View
Atman Binstock wrote:
> David Vandevoorde <vandevod@cs.rpi.edu> wrote:
[...]
> > A a;
> > a.template process<int>();
>
> Do you have to use the template key word?
> I thought
> a.process<int>();
> was the way you called it.
The template keyword is mandatory. Otherwise, the parser must
take `process' to be a non-template and the following `<' to
be a less-than operator ([temp.names] 14.2/4 in my copy of the
DWP).
The rules here are not always very intuitive. Take the following:
template<typename T>
struct S {
~S();
};
void
f() {
S<int>& p = ...;
p.A<int>::~A();
}
In this explicit destructor call, the template keyword is neither
required not allowed. I.e., this `template' prefix _only_ applies
to names of _member_ templates with explicit argument dependency.
Daveed
---
[ 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
]