Topic: Why templates do not support default arguments
Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Fri, 24 Dec 1993 09:53:10 GMT Raw View
dak@tabaqui.informatik.rwth-aachen.de (David Kastrup) writes:
>fjh@munta.cs.mu.OZ.AU (Fergus Henderson) writes:
>
>>grumpy@cbnewse.cb.att.com (Paul J Lucas) writes:
>
>>> Anyway, assuming:
>>>
>>> template<class T> void f( T t = 0 ) { }
>>>
>>> were legal and I did:
>>>
>>> f(); // huh?
>>>
>>> which f() would be called?
>
>>Presumeably the same f() that would be called if you wrote `f(0);',
>>i.e. f(int).
>
>No, that would be nonsensical.
I don't think so. I think if you assume that the construct is
legal, then the semantics must be those given above.
I don't see why these semantics are nonsensical,
and I don't see anything in the ARM which outlaws this construct.
>The function os written shorthand for:
>void f(T t = T(0)), meaning that the 0 is cast to type T, regardless
>what the type is.
No, that's not true. If that were true, then
void f(char *t = 1); // illegal
would be legal, just like
void f(char *t = (char *)1); // legal
is.
>The type of a function template must be figurable
>from its parameters. It simply cannot if the template type does not
>appear in the parameter list.
The ARM says "a match on a template implies that a specific
template function with arguments that exactly matches [sic] the
types of the arguments will be generated." I don't see why
the second occurence of "arguments" shouldn't include default
arguments as well as actual parameters.
>So the above example would (according
>to my C++ feeling) illegal [...]
According to my humble C++ feeling, the above example would be legal.
But I would certainly agree that the ARM is not very clear
about it. I think that this is something that the committee
will have to decide.
--
Fergus Henderson | "People who brook no compromise in programming
| languages should program in lambda calculus or
fjh@munta.cs.mu.OZ.AU | machine language, depending." --Andrew Koenig.