Topic: overloading templates
Author: cok@islsun.Kodak.COM (David Cok)
Date: 4 Apr 91 11:45:57 GMT Raw View
Will someone privy to the ANSI discussions on templates answer this for me?:
Is it expected that class template definitions will be able to be overloaded
or have default arguments? The ARM does not discuss this except to say that
"There must be exactly one definition for each template of a given name in a
program", which leads me to think the answer to my question is no. However,
a template is basically a function which returns a class or a function (it
just has to be evaluated at compile-time), so one might think that it could
be overloaded like ordinary functions.
For example:
template<class T> class Vector ...
// defines a template for a Vector of T of arbitrary
// and variable size
template<class T, int size> class Vector ...
// defines a template for a Vector of T of fixed size
// some checks on sizes of vectors could be done at
// compile-time
then
Vector<int> a,b;
Vector<int,10> c,d;
Vector<int,20> e,f;
...
a+=b; // runtime check that sizes of vectors are the same
c+=d; // ok, sizes match at compile-time
c+=e; // compile-time type error -- no operation defined to add a
// Vector<int,10> to a Vector<int,20>
Another example:
template<class T,int size=100> class Stack ...
// defines a Stack implemented on top of a fixed size array
// with default size of 100
Stack<int> a; // Stack of ints with size of 100
Stack<int,200> b; // Stack of ints with size of 200
David R. Cok
Eastman Kodak Company -- Imaging Science Laboratory
cok@Kodak.COM
Author: mjv@objects.mv.com (Michael J. Vilot)
Date: 10 Apr 91 11:43:29 GMT Raw View
>Message-ID: <1991Apr4.114557.17897@kodak.kodak.com>
>Sender: cok@Kodak.COM
>
>Is it expected that class template definitions will be able to be
overloaded
>or have default arguments?
The Annotated Reference Manual, p. 342: the grammar for
_template-argument_ includes _argument-declaration_, which has at
least one production allowing default arguments.
p. 343:
"A class template name must be unique in a program and may not be
declared to refer to any other template, class, function, object,
bvalue, or type in the same scope."
p. 345:
"A template function may be overloaded either by (other) functions of
its name or by (other) template functions of that same name."
Hope this helps,
Mike