Jump to content
  • Advertisement
_WeirdCat_

C++ Template class with two types but one :X

Recommended Posts

For editor purposes i want to change my matrix class which for now is


template <class T> class Matrix44 {

template <class T> class Matrix44

 

however i would like to multiply lets say Matrix44<float> with Matrix44<double> i could do this by adding


 

template <class T, class T2> class Matrix44

But this would require me to define every matrix44 as <float, double> etc. which is not an option since i have hundreds of such vars.  The change in code would be significant if not i would have to rewrite tenth thousands lines of code.

 

Maybe theres a quick solution to this

 

I woukld like to multiple world matrix by viewmatrix and projection matrix thus world matrix would be double and rest would be float

so for now i have 

Matrix44<T> operator *(Matrix44<T> mat) which forces me to use only one type.

There should be a way to use other type 

Matrix44<T> operator *(Matrix44<T2> mat)

 

 

any thoughts?

 

 

 

Share this post


Link to post
Share on other sites
Advertisement

Why do you not add a constructor to your matrix class for constructing a matrix from a given matrix with a different type; a generalized copy constructor? Do not make the constructor explicit and you will have implicit conversions for the arguments passed to your overloaded operators.

Edited by matt77hias

Share this post


Link to post
Share on other sites

Why not just stick to one type(float preferred) and remove the templatization? This is classic over-engineering with templates. Much of your code won't need a double type 4x4 matrix.

Share this post


Link to post
Share on other sites

Yeah, you could eigther add a templated copy ctor, or a templated multiplication operator:

template<typename T2>
Matrix(const Matrix<T2>& m)
{
}

template<typename T2>
Matrix operator*(const Matrix<T2>& m)
{
}

The ctor is more general and extensible, though can result in unwanted conversions, especially if you do not make it explicit.

Btw, out of interest, is there really a general compelling reason to have the world-matrix as double? I've never seen or heard of such a thing, always all matrices have had the same type of float.

Share this post


Link to post
Share on other sites

I personally wouldn't allow mixed-type operations like this to performed, at least not easily (or accidentally).

An explicit copy constructor that can convert between types provides a good balance between functionality and usability. Plus, there's only a single place in the code where you have to worry about type conversion issues, which is a plus:

template<typename U>
explicit Matrix(const Matrix<U>& m)
{
}

 

Share this post


Link to post
Share on other sites
18 hours ago, Zipster said:

An explicit copy constructor that can convert between types provides a good balance between functionality and usability. Plus, there's only a single place in the code where you have to worry about type conversion issues, which is a plus:

THB, if you are doing the explicit constructor I'd personally go for an explicit conversion method instead:

template<typename Type2>
Matrix<Type2> To(void) const
{
  return ...; // eigther have an explicit ctor public/private as well or define conversion here
}  

Might just be personal preference, but I find it easier to read/write:

Matrix<double> mWorld;

matrixTranslate(Matrix<float>(mWorld));
// VS
matrixTranslate(mWorld.To<float>());

don't know if there's some objective merit to prefering the explicit ctor, but I've been adopting the method-approach for my projects' math classes so far, just though I'd throw it in there.

Share this post


Link to post
Share on other sites

I would go with the templated multiplication operator suggested by @GDnetplus, it's narrowly targeted to support only the operations you want to support, and it follows a pattern you'll find in the standard library which makes it unsurprising.

Share this post


Link to post
Share on other sites
On 10/26/2017 at 7:29 AM, WiredCat said:

template <class T> class Matrix44 {

For matrix operations specifically this is rarely done in games.

In practice each form is heavily specialized, and often written with SIMD instructions unique to the type being created.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!