|
-
December 2nd, 2004, 02:54 AM
#1
Can we add two templates in c++
Hi guys
I have curiosity in my mind about templates.I just want to know that can we add two templates like we add two objects in c++.If we can add templates, then what is the procedure to add them.Plz tell me.
Thanks & Regards
Mayank Khare
-
December 2nd, 2004, 03:50 AM
#2
Re: Can we add two templates in c++
If you are refering the adding two template class instances where the template parameters are the same, it will be exaclty the same as adding two normal class instances that are of the same type. If the class provides an operator+(), then addition can be performed.
-
December 2nd, 2004, 10:26 AM
#3
Re: Can we add two templates in c++
Additionally (no pun intended), if you template the + operator itself, you can enable addition of different template instances also. Whether or not it would make sense to do so however, depends on the context.
Insert entertaining phrase here
-
December 3rd, 2004, 07:06 AM
#4
Re: Can we add two templates in c++
You can write a template default function for operator+ which makes use of += thus:
Code:
template < typename L, typename R >
L operator+( L l, const R& r )
{
l += r;
return l;
}
The first parameter could be const L& but then you'd have to invoke the copy constructor inside the body. This way does it automatically.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|