Speacialized operator for dissimilar types

J

James

I can't find a solution to this one, any suggestions would be welcome.

Say you have a matrix class specialized with storage type and
dimensions:

template<class T, unsigned ROWS, unsigned COLS>
class Matrix() {
...
};

You can overload an operation for multiplication of similar specialized
types:
Matrix<T,ROWS,COLS> operator*(const Matrix<T,ROWS,COLS>& right) const;

but can you think of a way to specialize a global function for
dissimilar types? ie:

Matrix<float, 3, 1> m1, m3;
Matrix<float, 3, 3> m2;
//Initialize operands...
m3 = m2 * m1;


TIA for any suggestions!
 
A

Alan Johnson

James said:
I can't find a solution to this one, any suggestions would be welcome.

Say you have a matrix class specialized with storage type and
dimensions:

template<class T, unsigned ROWS, unsigned COLS>
class Matrix() {
...
};

You can overload an operation for multiplication of similar specialized
types:
Matrix<T,ROWS,COLS> operator*(const Matrix<T,ROWS,COLS>& right) const;

but can you think of a way to specialize a global function for
dissimilar types? ie:

Matrix<float, 3, 1> m1, m3;
Matrix<float, 3, 3> m2;
//Initialize operands...
m3 = m2 * m1;


TIA for any suggestions!

Here is a test program that I think does what you are trying to do:

-Alan

#include <iostream>

template <typename T, unsigned ROWS, unsigned COLS>
class Matrix
{
} ;

template <typename T, unsigned A, unsigned B, unsigned C>
Matrix<T, A, C> operator *(
const Matrix<T, A, B> & m1,
const Matrix<T, B, C> & m2)
{
std::cout << "Multiplying " << A << 'x' << B << " Matrix by "
<< B << 'x' << C << " Matrix." << std::endl ;
return Matrix<T, A, C>() ;
}

int main()
{
Matrix<int, 2, 3> a ;
Matrix<int, 3, 4> b ;
Matrix<int, 2, 4> c ;

c = a * b ;

return 0 ;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top