Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   generic functions(template functions) (http://www.velocityreviews.com/forums/t447753-generic-functions-template-functions.html)

Tony Johansson 08-16-2005 09:59 AM

generic functions(template functions)
 
Hello Experts!

Assume I have one function that is a template function called add and one
concrete function that also have the name add is it any point to make a
concrete function add in this example?

template <typename T>
T add(T rh, T lh)
{ return rh+lh; }

template <typename T>
int add(int rh, int lh)
{ return rh+lh; }
int main()
{
int i=1, j=2;
cout << "result " << add<int>(i,j) << endl;

return 0;
}

//Many thanks

//Tony



Srini 08-16-2005 10:17 AM

Re: generic functions(template functions)
 
> Hello Experts!
>
> Assume I have one function that is a template function called add and one
> concrete function that also have the name add is it any point to make a
> concrete function add in this example?
>
> template <typename T>
> T add(T rh, T lh)
> { return rh+lh; }
>
> template <typename T>
> int add(int rh, int lh)
> { return rh+lh; }
> int main()
> {
> int i=1, j=2;
> cout << "result " << add<int>(i,j) << endl;
> return 0;
>
> }


In the above example there's no point for the template
specialization(That's the term you'd want to use). In general you'd
want to specialize a function template when you want to have different
behavior for particular types.

Srini


benben 08-16-2005 10:28 AM

Re: generic functions(template functions)
 

"Srini" <srinivasa.s@gmail.com> wrote in message
news:1124187449.652488.264850@g47g2000cwa.googlegr oups.com...
>> Hello Experts!
>>
>> Assume I have one function that is a template function called add and one
>> concrete function that also have the name add is it any point to make a
>> concrete function add in this example?
>>
>> template <typename T>
>> T add(T rh, T lh)
>> { return rh+lh; }
>>
>> template <typename T>
>> int add(int rh, int lh)
>> { return rh+lh; }
>> int main()
>> {
>> int i=1, j=2;
>> cout << "result " << add<int>(i,j) << endl;
>> return 0;
>>
>> }

>
> In the above example there's no point for the template
> specialization(That's the term you'd want to use). In general you'd
> want to specialize a function template when you want to have different
> behavior for particular types.
>
> Srini
>


The code by Tony didn't look to me like template specialization. A full
specialization of function template add<> would look like:

template <>
int add(int rh, int lh)
{
return rh + lh;
}



Srini 08-16-2005 10:33 AM

Re: generic functions(template functions)
 
> The code by Tony didn't look to me like template specialization. A full
> specialization of function template add<> would look like:
>
> template <>
> int add(int rh, int lh)
> {
> return rh + lh;
> }


Yeah - even I thought so. But I checked compiling both forms (What OP
has given and what you've given) and both gave same results. I thought
that specialization was what the OP meant in his post.



All times are GMT. The time now is 01:15 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.