By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,922 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,922 IT Pros & Developers. It's quick & easy.

Speacialized operator for dissimilar types

P: n/a
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!

Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
James wrote:
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 ;
}
Jul 23 '05 #2

P: n/a
This does exactly what I want. I'll give it a shot, thanks Alan.

James

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.