Advertisement

partial templates

Started by February 01, 2001 06:01 PM
4 comments, last by Succinct 23 years, 11 months ago
say i''ve got 3 classes w/ very similar uses, such as a 3d and 4d vector. say each class has a .Add( T ) function is it possible to write 1 template function that can perform this function on either of the two classes using any parameter T? basically, i want to define the first part of a multi-parameter template function to be one of just a couple of specialized classes, like:
  
template<class Vector(3 or 4d),class T> Vector operator +( Vector v,T t )
{
     return v.Add( t );
}
  
any ideas? Thank you for your bandwidth. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ I have no name that you may call me. I am merely Succinct. ~Succinct Demos Online~ " I''m suprised nobody takes M$ to court for rape... The OS keeps going down on you w/o your permission."
-- Succinct(Don't listen to me)
You can specify classes as template parameters.
  template<class V, class T>class Vector//and when you use itVector<<Vector3D<T>,T>>>//there's a bug in MSVC, and I had to add a third > on the end to make it compile//But you still need to write code for each case...  


If you used a dynamic array for the vector elements, you'd have to loop through each element to do math ops - this is very bad for performance. That's why you write a vector3D & a vector4D class. You might be able to always use the 4D and set the 4th D to 0...

Magmai Kai Holmlor
- The disgruntled & disillusioned


Edited by - Magmai Kai Holmlor on February 1, 2001 12:54:31 AM
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Advertisement
i meant more along the lines of having a Vector3d and a Vector4d, but sharing similar operators.

basically i just want to write a global: V operator +(const T&) where type T is any arbitrary type, and type V is either of the vector types.

for instance, if i give each class an Add() series of overloaded functions, the operator ''+'' would be exactly the same for both, because it would just call the vector''s .Add function

so i figure, write a template that can add an Vector type (of the 2), and any other arbitrary type that i''ve defined an appropriate overloaded Add() function for, but i can''t find the syntax for writing a template that uses one completely unspecialzed parameter, and one parameter that is specialized for two types, but can''t be specialized or generalized beyond that.

ur right about msvc++ and that stupid bug, you can''t do the Strostrup-style template type conversions in it, or so i thought until i saw your work-around



thx for trying, though


Thank you for your bandwidth.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~Succinct Demos Online~


"&ltDiLLiGaS> I''m suprised nobody takes M$ to court for rape... The OS keeps going down on you w/o your permission."
-- Succinct(Don't listen to me)
i was also thinking of creating a base vector type, that would abstract the specific vector type, but then vtables and indirection popped into my head and i''m trying to keep these as speedy as possible...

Thank you for your bandwidth.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~Succinct Demos Online~


"&ltDiLLiGaS> I''m suprised nobody takes M$ to court for rape... The OS keeps going down on you w/o your permission."
-- Succinct(Don't listen to me)
Use normal template parameters and let the compiler disallow classes that don''t have Add member functions.



template &lttypename V, typename T> inline V operator +( V& v,T& t
{
return v.Add( t );
}



Why do you need to specialize it?
hrmm, but that's going to be a global + operator, will the compiler allow it?

*goes and tries*



Edited by - Succinct on February 5, 2001 11:34:19 AM
-- Succinct(Don't listen to me)

This topic is closed to new replies.

Advertisement