Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 16th, 2005, 11:05 AM
Tony Johansson
Guest
 
Posts: n/a
Default 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


  #2  
Old August 16th, 2005, 11:25 AM
Srini
Guest
 
Posts: n/a
Default Re: generic functions(template functions)

> Hello Experts![color=blue]
>
> 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;
>
> }[/color]

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

  #3  
Old August 16th, 2005, 11:35 AM
benben
Guest
 
Posts: n/a
Default Re: generic functions(template functions)


"Srini" <srinivasa.s@gmail.com> wrote in message
news:1124187449.652488.264850@g47g2000cwa.googlegr oups.com...[color=blue][color=green]
>> 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;
>>
>> }[/color]
>
> 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
>[/color]

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;
}


  #4  
Old August 16th, 2005, 11:45 AM
Srini
Guest
 
Posts: n/a
Default Re: generic functions(template functions)

> The code by Tony didn't look to me like template specialization. A full[color=blue]
> specialization of function template add<> would look like:
>
> template <>
> int add(int rh, int lh)
> {
> return rh + lh;
> }[/color]

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.

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles