# Imposing a condition on the vector addition of two different vector types

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

## Recommended Posts

Well, I've written a Vector3 classe...It's a class template and here is a forward declaration:
template<typename Real,class MathLib> class Vector3;


Real is of course a built-in float type and MathLib is a class with static functions and variables only( policy class? ) that implements some algorithms. I initialy overloaded the most common operators that way:
template<typename Real,class MathLib>
class Vector3
{
public:
...
const Vector3 operator+( const Vector3<Real,MathLib>& )const;

private:
Real x[3];
};


This is actually a limited design since the operator ares only defined for class templates with the same ( template ) parameters...A more general design would be:
template<typename Real,class MathLib>
class Vector3
{
public:
...
template<class Vector>
const Vector3 operator+( const Vector& v )const
{
return Vector3( x[0] + v[0], x[1] + v[1], x[2] + v[2] );
}
};


That's more general except that I want to be able to impose properties on the template parameter( Vector and Matrix ), like dimensions. Without them I can add a Vector3 and a Vector4, since the latter has at least three elements. The first idea I had is to define a static variable and to test inside the "+" operator of template<typename Real,class Math> Vector3, if the dimensions of the vectors are equal.
templaye<typename Real,class Math>
class Vector3
{
public:
const static int DIMENSION;
...
};

template<typename Real,class Math>
template<Vector>
const Vector3<Real,Math> Vector3<Real,Math>::operator+( const Vector& v )const
{
assert( Vector::DIMENSION == Vector3::DIMENSION )

return Vector3<Real,Math>( x[0] + v[0], x[1] + v[1], x[2] + v[2] );
}

template<typename Real,class Math> int const Vector3<Real,Math>::DIMENSION = 3;


Is a there another( more elegant ) way to solve the problem?

##### Share on other sites
Of the many, many, many (frankly, way too many) game math libraries I've seen, the best ones use a fair amount of copy-paste reuse. Remember, elegance in an implementation is important, but elegance in the interface is infinitely more so. Moreover, the more you play with Vector2, Vector3, and Vector4, the less alike they will look. Just make them different classes.

##### Share on other sites
Quote:
 Original post by SneftelOf the many, many, many (frankly, way too many) game math libraries I've seen, the best ones use a fair amount of copy-paste reuse. Remember, elegance in an implementation is important, but elegance in the interface is infinitely more so. Moreover, the more you play with Vector2, Vector3, and Vector4, the less alike they will look. Just make them different classes.

They are...and with the way I've implemented my Vector3 class, I can have as many Vector3 types as policy classes. I simply want to avoid adding 2 vectors of different lengths, in my template overloaded operator+. I want to be able to add two Vector of the same length but of different types, elegantly. The idea I had came naturally but I just want to know if there is a more elegant way to achieve the goal...

##### Share on other sites
Then simply parameterize on the parameters of the Vector3 template, rather than on the vector type as a whole.

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 18
• 20
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633363
• Total Posts
3011514
• ### Who's Online (See full list)

There are no registered users currently online

×