Topic: Seeking rationale of template definition syntax
Author: "Jim Cobban" <Jim.Cobban.jcobban@nt.com>
Date: 1998/12/10 Raw View
Having successfully created my first few template definitions I find the
syntax of the definitions curiously unintuitive. In particular it seems
strange to me that although a template name is always used with a set of
parameters enclosed in <> brackets, it is not declared that way.
That is by contrast with how functions are declared and used as follows:
int func(a_type a_parm) { ... }
a_type a_instance;
int i = func(a_instance);
a template is declared and used as follows:
template<a_type a_parm> Tclass { ... };
a_type a_instance;
Tclass<a_instance> something;
Even though everywhere that Tclass is subsequently used it must be followed
by a supplied parameter enclosed in <> brackets, in the declaration the
parameters are placed after the word template, not after the class name.
To me it would have seemed more intuitive to specify:
template Tclass<a_type a_parm> { ... };
Could someone explain the rationale behind the chosen syntax?
Similarly I find the declaration syntax for functions which act on templates
to be cumbersome. Why do I have to say:
template<a_type a_parm> int tfunc(Tclass<a_parm> tparm);
when the compiler already knows that Tclass is a template taking a parameter
of type a_type. That is why can I not just say:
int tfunc(Tclass<a_parm> tparm);
I have not yet been able to figure out how to declare a friend function
taking template parameters. When I insert a definition:
friend operator << (ostream &, const Tclass<a_parm> tparm);
in a template class G++ puts out five lines of warnings. So I have had to
create a clumsy kluge using a method returning a string which can be used by
the ostream inserter so it does not have to be a friend of the template
class.
--
Jim Cobban | jcobban@nortel.ca | Phone: (613) 763-8013
Nortel Networks (MED) | FAX: (613) 763-5199
[ 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: "Matt Seitz" <mseitz@meridian-data.com>
Date: 1998/12/11 Raw View
Jim Cobban wrote in message <74pdif$9f7$1@bcarh8ab.ca.nortel.com>...
>Similarly I find the declaration syntax for functions which act on
templates
>to be cumbersome. Why do I have to say:
>
> template<a_type a_parm> int tfunc(Tclass<a_parm> tparm);
>
>when the compiler already knows that Tclass is a template taking a
parameter
>of type a_type. That is why can I not just say:
>
> int tfunc(Tclass<a_parm> tparm);
You can say either one, but they have two different meanings. The first:
template<a_type a_parm> int tfunc(Tclass<a_parm> tparm);
means "Create a function called tfunc that can accept a Tclass based on any
type."
The second:
int tfunc(Tclass<a_parm> tparm);
means "create a function called tfunc that can accept only a Tclass based on
type a_parm"
Here's another example:
void sort(vector<int> list); //Declares a function that sorts a list of
ints (vector<int>)
template <class VType> void sort( vector<VType> list); //Declares a function
that can sort a list of any type
[ 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 ]