Archived

This topic is now archived and is closed to further replies.

Different types in templates

This topic is 5114 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have some templates that I use for calculations. Sometimes I use them for integers and sometimes for floating point. If I have something like this:
template <class Type>
	const Matrix44<Type>& Matrix44<Type>::rotation(Vector3<Type> s, Vector3<Type> t)
	{
		Type h = (3 * s.x);

// not the end

Now, if I use float I want the number 3 to be converted to a float already at compile time. Is it possible to control this? Would it help if I make a cast to Type?

Share this post


Link to post
Share on other sites
Hi fold

Well you shouldnt really use magic numbers, its not really clear as to what the 3 is doing. You may try something like this, as it would solve the magic number and the type of the magic number problems


template<typename T>
void func()
{
const T height = 3.f;
...
T h = (height * s.x);
...
}



or if you really wanted to use the magic number, you may want to use a static_cast


template<typename T>
void func()
{
...
T h = ( static_cast<T>(3.0f) * s.x);
...
}


Hope that helps



[edited by - Jingo on December 16, 2003 8:14:26 AM]

Share this post


Link to post
Share on other sites