#### Archived

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

# Different types in templates

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

## 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 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 on other sites
That was exactly what I wanted. Thanks!

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 12
• 10
• 13
• 104
• 11
• ### Forum Statistics

• Total Topics
632979
• Total Posts
3009683
• ### Who's Online (See full list)

There are no registered users currently online

×