dimanche 12 avril 2015

C++ - same static method for any type of a template class


Vote count:

0




I have 2 templated classes, Matrix4x4t and Vector3t, and a number of static methods in Matrix4x4t that operate on them both. Matrix4x4t is defined as follows, with one of said static methods included:



template <class T>
class Matrix4x4t
{
public:
T values[16];
...
template <class W, class U, class V> static void multiply3x3Vector3(const Matrix4x4t<W> &m, const Vector3t<U> &columnVector, Vector3t<V> &result);
...
};


The idea of multiply3x3Vector3 is to take any possible type of matrix (m) and vector (columnVector), perform multiplication, then store the result in a vector (result), converting to it's type if different. This works well and allows me to do the following:



Matrix4x4t<double> matd;
Vector3t<float> vec3f(1.0f, 2.0f, 3.0f);
Vector3t<int> resulti;

Matrix4x4t<float>::multiply3x3Vector3(matd, vec3f, resulti); //This works
Matrix4x4t<double>::multiply3x3Vector3(matd, vec3f, resulti); //So does this


The issue is that if I'm understanding correctly, the compiler will create 2 separate definitions of multiple3x3Vector3, one for Matrix4x4t<int> and another for Matrix4x4t<double>, even though they take the same types and do the same thing. It's probably also unclear for the user why they need to use a specific type of Matrix4x4t for each call when they produce the same result, and it would be nicer if they could just call:



Matrix4x4t::multiply3x3Vector3(matd, vec3f, resulti);


The solution to both is to make multiple3x3Vector3 a function separate from the class itself, but I like having it inside the scope of Matrix4x4t. I could have static methods that simply call an external function, but before I go this route I thought I'd ask if there is something I'm missing that would solve the two issues and let me keep the method inside the class?



asked 5 secs ago







C++ - same static method for any type of a template class

Aucun commentaire:

Enregistrer un commentaire