C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

temlate deduction from default parameter

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Christof Warlich
Guest





PostPosted: Thu Apr 12, 2007 10:53 pm    Post subject: temlate deduction from default parameter Reply with quote



Hi,

is there any good reason why template deduction from a default parameter
does not work? E.g., the following does not compile:

template<typename T> void x(const T &t = (int) 0) {
// do something:
(void) t;
}

int main(void) {
x();
}

Thanks,

Christof

---
[ 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 ]
Back to top
Mathias Gaunard
Guest





PostPosted: Fri Apr 13, 2007 4:54 am    Post subject: Re: temlate deduction from default parameter Reply with quote



On Apr 12, 7:53 pm, cwarl...@alcatel-lucent.de (Christof Warlich)
wrote:
Quote:
Hi,

is there any good reason why template deduction from a default parameter
does not work? E.g., the following does not compile:

template<typename T> void x(const T &t = (int) 0) {
// do something:
(void) t;

}

int main(void) {
x();

}

How could C++ know that it is that template that should be called?
What is there are other overloads of x ?

---
[ 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 ]
Back to top
Greg Herlihy
Guest





PostPosted: Fri Apr 13, 2007 8:42 am    Post subject: Re: temlate deduction from default parameter Reply with quote



On 4/12/07 10:53 AM, in article 461e38fe$1 (AT) news (DOT) alcatel.com, "Christof
Warlich" <cwarlich@alcatel-lucent.de> wrote:

Quote:
Hi,

is there any good reason why template deduction from a default parameter
does not work? E.g., the following does not compile:

template<typename T> void x(const T &t = (int) 0) {
// do something:
(void) t;
}

int main(void) {
x();
}

The compiler deduces the type of the template parameter from the type of
argument passed to the function call. So in this example, the compiler has
to deduce type "T" from the parameter that was _not_ passed in x's function
call. So the question essentially becomes what is the type of the argument
that is not present in main()'s call to x()?

In C++09x that question may actually have an answer - in the form of a
default template type parameter:

template < class T = int >
void x( const T& t = 0 )
{
...
}

int main()
{
x(); // OK (...someday)
}

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 ]
Back to top
Christof Warlich
Guest





PostPosted: Fri Apr 13, 2007 6:37 pm    Post subject: Re: temlate deduction from default parameter Reply with quote

Quote:
template<typename T> void x(const T &t = (int) 0) {
// do something:
(void) t;

}

int main(void) {
x();

}

How could C++ know that it is that template that should be called?
What is there are other overloads of x ?

I thought this is obvious: The declaration of x() has (int) 0 as
default parameter, so I'd have expected that the call to x() would
instantiate x() for type int and call it with the value 0.

The notation suggested by Greg as an outlook to C++09x would be
fine from my point of view as well, but I would think the above
should work either, as it emphazises the deduction of the type
from its (default) parameter, similar to a real parameter.

Did I miss some important aspect?

---
[ 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 ]
Back to top
Greg Herlihy
Guest





PostPosted: Sat Apr 14, 2007 9:10 am    Post subject: Re: temlate deduction from default parameter Reply with quote

On 4/13/07 6:37 AM, in article 461f1c74 (AT) news (DOT) alcatel.com, "Christof Warlich"
<cwarlich@alcatel-lucent.de> wrote:

Quote:
template<typename T> void x(const T &t = (int) 0) {
// do something:
(void) t;

}

int main(void) {
x();

}

How could C++ know that it is that template that should be called?
What is there are other overloads of x ?

I thought this is obvious: The declaration of x() has (int) 0 as
default parameter, so I'd have expected that the call to x() would
instantiate x() for type int and call it with the value 0.

A default argument provides a default value for a parameter in a function
call - but it does not provide an argument with a deducible type. Or to put
it another way: the parameter t in function x()'s declaration already has a
specified type: T. So all that a default argument does is to tell the
compiler to initialize t with the int value 0 (whenever no argument is
provided in a function call). Be even with a default value for t, the
compiler still needs to know t's type before it can assign it the default
value (in the case the int value, 0). The following function call helps to
illustrate this distinction between a parameter's type and the value that
may initialize it:

x<long>(); // long parameter is initialized with the int value 0

Quote:
The notation suggested by Greg as an outlook to C++09x would be
fine from my point of view as well, but I would think the above
should work either, as it emphazises the deduction of the type
from its (default) parameter, similar to a real parameter.

No, the compiler must have determined t's type, T, before t can be assigned
a default value, 0. In practical terms, this means that x() must be
instantiated before the default argument can even be considered. But a
function call to x() without any arguments leaves the compiler with no type
with which to instantiate x() (at least not until default type arguments for
function templates come along). Therefore, x<int>() is not instantiated and
does not become a candidate function that could resolve the function call.

To attain the desired behavior in the current C++ language standard, just
implement a "helper" function to forward the call:

inline void x() { x<int>(0); }

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 ]
Back to top
Guest






PostPosted: Sat Apr 14, 2007 6:35 pm    Post subject: Re: temlate deduction from default parameter Reply with quote

Greg Herlihy wrote:
Quote:
On 4/13/07 6:37 AM, in article 461f1c74 (AT) news (DOT) alcatel.com, "Christof Warlich"
cwarlich@alcatel-lucent.de> wrote:

template<typename T> void x(const T &t = (int) 0) {
// do something:
(void) t;

}

int main(void) {
x();

}

How could C++ know that it is that template that should be called?
What is there are other overloads of x ?

Obviously, this would only be feasible if there were rules that ranked
the possible matching overloads, and if it were undefined behavior if
there were two or more different best matches. The standard provides
plenty of precedent for rules of that kind.

Quote:
I thought this is obvious: The declaration of x() has (int) 0 as
default parameter, so I'd have expected that the call to x() would
instantiate x() for type int and call it with the value 0.

A default argument provides a default value for a parameter in a function
call - but it does not provide an argument with a deducible type. Or to put
it another way: the parameter t in function x()'s declaration already has a
specified type: T. So all that a default argument does is to tell the
compiler to initialize t with the int value 0 (whenever no argument is
provided in a function call). Be even with a default value for t, the
compiler still needs to know t's type before it can assign it the default
value (in the case the int value, 0). The following function call helps to
illustrate this distinction between a parameter's type and the value that
may initialize it:

x<long>(); // long parameter is initialized with the int value 0

The notation suggested by Greg as an outlook to C++09x would be
fine from my point of view as well, but I would think the above
should work either, as it emphazises the deduction of the type
from its (default) parameter, similar to a real parameter.

No, the compiler must have determined t's type, T, before t can be assigned
a default value, 0. In practical terms, this means that x() must be
instantiated before the default argument can even be considered. But a
function call to x() without any arguments leaves the compiler with no type
with which to instantiate x() (at least not until default type arguments for
function templates come along). Therefore, x<int>() is not instantiated and
does not become a candidate function that could resolve the function call.

That's a good description of the way the rules are currently written;
but as far as I can tell, it would have been feasible to write them to
allow type deduction from default arguments. It would require a major
re-write of the type deduction rules, but is there any other reason
why it would be a bad idea? I'm not saying that there's no such
reason; just pointing out that explaining that reason would have been
a better answer to this inquiry than simply explaining what the
current rules are.

---
[ 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 ]
Back to top
Christof Warlich
Guest





PostPosted: Tue Apr 17, 2007 6:42 pm    Post subject: Re: temlate deduction from default parameter Reply with quote

Quote:
That's a good description of the way the rules are currently written;
but as far as I can tell, it would have been feasible to write them to
allow type deduction from default arguments. It would require a major
re-write of the type deduction rules, but is there any other reason
why it would be a bad idea? I'm not saying that there's no such
reason; just pointing out that explaining that reason would have been
a better answer to this inquiry than simply explaining what the
current rules are.
Thanks for this summary, it perfectly wraps up the intention of my post.

Up to now, the answer to my initial question:

"is there any _good_ reason why template deduction from a default
parameter does not work?"

seems to be that the _current_ rules do not permit it. A more satisfying
answer would have been something like "interesting point, but not
feasable because ....." or "bad idea, because it may cause problems ..."
or (my favorite Smile): "good idea, let's consider it for the future
standard".

Christof

---
[ 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 ]
Back to top
Greg Herlihy
Guest





PostPosted: Thu Apr 19, 2007 5:00 am    Post subject: Re: temlate deduction from default parameter Reply with quote

On 4/17/07 6:42 AM, in article 4624708d$1 (AT) news (DOT) alcatel.com, "Christof
Warlich" <cwarlich@alcatel-lucent.de> wrote:

Quote:
"is there any _good_ reason why template deduction from a default
parameter does not work?"

Yes. C++2003 prohibited function templates from having default template type
arguments - or anything (such as type deduction from default argument) that
would have provided a similar mechanism.

As it turned out (see
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#226 ) there was
no compelling reason why function templates should not support default type
parameters, so C++2009 will allow them - and does with using the syntax that
already exists for specifying a default template type parameter.

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.

So in short, type deduction from default function arguments was banned in
the past and would be a counter-productive addition to C++2009.

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 ]
Back to top
Guest






PostPosted: Thu Apr 19, 2007 11:49 am    Post subject: Re: temlate deduction from default parameter Reply with quote

Greg Herlihy wrote:
..
Quote:
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.

It seems to me that you can always turn off template argument
deduction from a default function argument by explicitly specifying
the template argument. Could you provide an example to clarify the
problem this would cause?

---
[ 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 ]
Back to top
Sylvester Hesp
Guest





PostPosted: Thu Apr 19, 2007 3:31 pm    Post subject: Re: temlate deduction from default parameter Reply with quote

"Greg Herlihy" <greghe (AT) pacbell (DOT) net> wrote in message
news:C24BFD46.7CB8%greghe (AT) pacbell (DOT) net...
Quote:
[...]
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
}

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".

- Sylvester Hesp


---
[ 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 ]
Back to top
Greg Herlihy
Guest





PostPosted: Thu Apr 19, 2007 4:39 pm    Post subject: Re: temlate deduction from default parameter Reply with quote

On 4/19/07 3:31 AM, in article 46274000$0$329$e4fe514c (AT) news (DOT) xs4all.nl,
""Sylvester Hesp"" <s.hesp (AT) oisyn (DOT) nl> wrote:
Quote:
"Greg Herlihy" <greghe (AT) pacbell (DOT) net> wrote in message
news:C24BFD46.7CB8%greghe (AT) pacbell (DOT) 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.

Quote:
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 ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.