Topic: Question on function template
Author: Brijesh kant <bjsh_22@yahoo.co.uk>
Date: Fri, 1 Jun 2007 18:26:07 CST Raw View
--0-1687763346-1180635440=:543
Content-Type: text/plain; charset=ascii
I recently came to know that the default template paramater in the function templates are not allowed in c++. While the default template paramater is allowed in class template. Is there any specific reason for not including this functionality in the function template.
For example the sample code below will not compile in c++ :
template < class T = int >
T function ( T a )
{ return a+1; }
main()
{
function<>( 10 );
}
I was wondering as to why this is included in the c++ standards.
Thanks & Regards,
Brijesh Kant.
___________________________________________________________
Yahoo! Mail is the world's favourite email. Don't settle for less, sign up for
your free account today http://uk.rd.yahoo.com/evt=44106/*http://uk.docs.yahoo.com/mail/winter07.html
--0-1687763346-1180635440=:543
Content-Type: text/html; charset=ascii
<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:times new roman, new york, times, serif;font-size:12pt"><DIV></DIV>
<DIV>
<DIV>I recently came to know that the default template paramater in the function templates are not allowed in c++. While the default template paramater is allowed in class template. Is there any specific reason for not including this functionality in the function template. </DIV>
<DIV>For example the sample code below will not compile in c++ :</DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV> template < class T = int ></DIV>
<DIV> T function ( T a )</DIV>
<DIV> { return a+1; }</DIV>
<DIV> </DIV>
<DIV> main()</DIV>
<DIV>{</DIV>
<DIV> function<>( 10 );</DIV>
<DIV> }</DIV>
<DIV><BR>I was wondering as to why this is included in the c++ standards.</DIV>
<DIV> </DIV><BR> </DIV>Thanks & Regards,<BR>Brijesh Kant.
<DIV></DIV></div><br>
<hr size=1>
Yahoo! Answers - Got a question? Someone out there knows the answer. <a
href="http://uk.answers.yahoo.com/;_ylc=X3oDMTEzaDRkZGdlBF9TAzIxMTQ3MTcxOTAEc2VjA01haWwEc2xrA3RhZ2xpbmVz">Try
it now</a>.</body></html>
--0-1687763346-1180635440=:543--
---
[ 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 ]
Author: AlbertoBarbati@libero.it (Alberto Ganesh Barbati)
Date: Sun, 3 Jun 2007 15:11:21 GMT Raw View
Brijesh kant ha scritto:
> I recently came to know that the default template paramater in the
> function templates are not allowed in c++. While the default template
> paramater is allowed in class template. Is there any specific reason for
> not including this functionality in the function template.
This is true in C++03, but will change in C++0x. Default template
parameters are going to be allowed also on function templates.
> For example the sample code below will not compile in c++
>
>
> template < class T = int >
> T function ( T a )
> { return a+1; }
>
> main()
main() *must* return int and you *can't* omit the return value. Some
permissive compilers allows that, but you shouldn't rely on such
non-conforming behaviour.
> {
> function<>( 10 );
> }
Well... this is not the right example to show that, because the template
parameter is being deduced. In fact if you write:
template <class T = float> // C++0x
T function(T a)
{ return a + 1; }
int main()
{
function(10); // instantiates function<int>, not function<float>!
}
As 10 is an int, T is deduced to be int even if you have a default
template parameter. Default template parameters can be useful for return
values and non-deduced contexts:
template <class T = int> // C++0x
T function();
template <class T>
struct identity
{
typedef T value;
};
template <class T = int> // C++0x
T nondeduced(identity<T>::value a); // T is not deducible from a
int main()
{
function(); // T = int (because of default parameter)
function<float>(); // T = float (explicitly specified)
nondeduced(10); // T = int (because of default parameter)
nondeduced(10.f); // T = int (same reason)
nondeduced<float>(10); // T = float (explicitly specified)
}
HTH,
Ganesh
---
[ 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 ]
Author: Rupesh <rupesh.technical97@gmail.com>
Date: Thu, 7 Jun 2007 11:37:54 CST Raw View
On Jun 2, 5:26 am, Brijesh kant <bjsh...@yahoo.co.uk> wrote:
> I recently came to know that the default template paramater in the function templates are not allowed in c++. While the default template paramater is allowed in class template. Is there any specific reason for not including this functionality in the function template.
> For example the sample code below will not compile in c++ :
>
> template < class T = int >
> T function ( T a )
> { return a+1; }
>
> main()
> {
> function<>( 10 );
> }
>
> I was wondering as to why this is included in the c++ standards.
>
> Thanks & Regards,
> Brijesh Kant.
>
> ___________________________________________________________
> Yahoo! Mail is the world's favourite email. Don't settle for less, sign up for
> your free account todayhttp://uk.rd.yahoo.com/evt=44106/*http://uk.docs.yahoo.com/mail/winte...
It is properly compling in VC++ 6.0
Thanks& Regards,
Rupesh Shingavi,
Seed InfotechLtd
---
[ 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 ]
Author: Alberto Ganesh Barbati <AlbertoBarbati@libero.it>
Date: Fri, 8 Jun 2007 13:29:33 CST Raw View
Rupesh ha scritto:
> On Jun 2, 5:26 am, Brijesh kant <bjsh...@yahoo.co.uk> wrote:
>> For example the sample code below will not compile in c++ :
>>
>> template < class T = int >
>> T function ( T a )
>> { return a+1; }
>>
>> main()
>> {
>> function<>( 10 );
>> }
>>
>>
> It is properly compling in VC++ 6.0
Really? Well, it shouldn't. The code is ill-formed. In fact it doesn't
compile even with VC++ 7.1.
Ganesh
PS: The fact that a program compiles on *one* compiler can not be used
to prove or disprove its well-formedness. This is especially true for
VC++ 6.0, which is so far from being standard compliant...
---
[ 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 ]