Re: temlate deduction from default parameter
On 4/19/07 3:31 AM, in article 46274000$0$329$e4fe514c@news.xs4all.nl,
""Sylvester Hesp"" <s.hesp@oisyn.nl> wrote:
"Greg Herlihy" <greghe@pacbell.net> wrote in message
news:C24BFD46.7CB8%greghe@pacbell.net...
[...]
Therefore in C++2009, type deduction from a default function template
would
be completely redundant (since C++ already has a much more familiar syntax
for achieving the same result). But the consequences of adding this
behavior
would be worse then creating a redundant syntax. Because - once type
deduction is enabled from a default argument - there would be no apparent
mechanism for turning it off. In other words, if a program wants to
initialize a template argument to some specified value - but still require
the program to provide the parameter's type explicitly - then that program
would have lost the ability able to do so.
I don't really understand what you're saying.
template<class T>
void foo(T t = 12);
int main()
{
foo(); // call foo<int>(12), T is deduced from it's default argument
foo(23.f); // call foo<float>(23.f), T is deduced from the supplied
argument
foo<float>(); // call foo<float>(12.f), T is not deduced but
explicitely specified
}
In C++2009, the same three functions are supported with a default template
type parameter, like so:
template <class T = int>
void foo( T t = 12 );
...
foo(); // #1 OK calls foo<int>(12)
foo<float>(); // #2 OK calls foo<float(12.0f)
foo( 23.0f); // #3 OK calls foo<float>(23.0f)
Now in C++2009, it is possible for a program to "finetune" foo()'s supported
interface, say, by explicitly allowing calls #2 and #3 above - but rejecting
call #1:
template <class T>
void foo( T t = 12 );
...
foo(); // #1 Error: type T not deduced
foo<float>; // #2 OK calls foo(12.0f)
foo( 23.0f); // #3 OK calls foo<float>(23.0f)
Now assume for a moment that C++2009 performed type deduction from a default
function argument. So with this hypothetical change, the programmer will
have lost the ability to disable foo() call #1 while still allowing calls #2
and #3 (and would have gained nothing to offset losing this ability).
In other words, a programmer who wants to require foo's callers to supply
either a type or a value - can enforce such a requirement in C++2009. But
with type deduction from a default function argument, the same programmer
would be out of luck: a requirement to supply either a type or a value when
calling foo() would wind up having to allow calls to foo() that supply
neither.
If type deduction from default arguments is enabled, that doesn't
necessarily imply that it's _always_ deduced from it's default argument.
Only when both the argument itself and it's type aren't specified. So I'm
not sure what you mean by "there would be no apparent mechanism for turning
it off".
The point is that with type deduction from default arguments enabled, the
type would always be deduced (from somewhere). So there would be no way to
to turn off type deduction when supplying a default function argument. Yet,
as demonstrated above this kind of type deduction is not only not needed, it
is entirely unhelpful. Furthermore type deduction of default function
arguments runs counter to the the purpose of default function arguments as
they were designed (and as they works today). A default function exists to
provide a value for a parameter of a specified type - and to specify
anything about the parameter's type itself. And there is no reason for
C++2009 to lose the ability to make this useful distinction.
Greg
---
[ 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://www.comeaucomputing.com/csc/faq.html ]