I'm really unexperienced, so maybe it's nonsense. But I would like to know whether is there a possibility to use many different templates in one method or operator like:
template < class T, int x, int y> class CMatrix
{
// blah blah blah...
}
template < class T, int x, int z>
template < class T, int y, int z>
I know, syntax is not correct, template keyword shouldn't be used like above, but if You know how to handle this problem I appreciate any advice.
Gabriel Fleseriu
April 2nd, 2003, 03:09 AM
Don't make the matrix sizes template parameters.
PaulWendt
April 2nd, 2003, 06:37 AM
I don't even understand the question. You ask about using
multiple templates, but your example has only one template.
Do you mean multiple template parameters? If so, yeah, you can
use more than one template parameter. Look at your compiler's
implementation of the C++ Standard Library to see some
examples; even std::vector uses multiple template parameters.
--Paul
Graham
April 2nd, 2003, 07:26 AM
The following compiles:
template <typename T, int x, int y>
class Matrix
{
public:
template <int z>
Matrix<T, x, z> operator*(const Matrix<T, y, z>& m);
};
template <typename T, int x, int y, int z>
Matrix<T, x, z> Matrix<T, x, y>::operator*(const Matrix<T, y, z>& m)
{
}
However, I haven't tested it to see if the compiler will do the correct resolution operations on it in use.
Graham
April 2nd, 2003, 07:34 AM
I just tried the following (with the above definitions) in VC++6.0
and got an internal compiler error. So, no surprises there. VC++6 is notoriously bad at handling templates properly.
I compiled it with Comeau and it said
"ComeauTest.c", line 11: error: declaration is incompatible with function template
"Matrix<T, x, z> Matrix<T, x, y>::operator*(const Matrix<T, y, z> &)"
(declared at line 7)
Matrix<T, x, z> Matrix<T, x, y>::operator*(const Matrix<T, y, z>& m)
So I guess that isn't correct, either. I'll have a look and see if Bjarne's got the answer.
Graham
April 2nd, 2003, 07:40 AM
The answer is:
template <typename T, int x, int y>
class Matrix
{
public:
template <int z>
Matrix<T, x, z> operator*(const Matrix<T, y, z>& m);
};
template <typename T, int x, int y>
template <int z>
Matrix<T, x, z> Matrix<T, x, y>::operator*(const Matrix<T, y, z>& m)
{
}
BTW: VC++6 can't cope with this, but it is correct according to Comeau.
galathaea
April 2nd, 2003, 09:55 AM
Originally posted by Gabriel Fleseriu
Don't make the matrix sizes template parameters.
Using non-type template parameters for matrix size can be useful for optimizations. In particular, you can force inline easier, make decisions on algorithm choice based on size without leaving costly conditionals in the compiled code, and even allow the compiler to deal with some of the information which is known at translationtime. Unfortunately, it does eliminate the possibility of runtime dynamic matrices, but many of the calculations and simulations I have been involved with actually did not require that type of flexibility.
carnaz, Graham's excellent answer should get you started. Note that it is a parametrized method of a parametrized class, and that the class template parameters come before the additional parameter in the specification. You only need to declare the further abstraction needed to complete the signature in the method's template parameter, and not any of the full <> collections as in your posted attempt, as the type matching will be done throughout.
Graham
April 2nd, 2003, 12:58 PM
I feel that I ought to point out that a work-around for VC++6 is to put the function body inside the class definition, rather than outside:
template <typename T, int x, int y>
class Matrix
{
public:
template <int z>
Matrix<T, x, z> operator*(const Matrix<T, y, z>& m)
{
// .....
}
};
Makes the class declaration harder to read, but it should compile (haven't tested it, though).
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.