|
-
April 2nd, 2003, 03:04 AM
#1
more templates at once?
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>
CMatrix < T, x, z> CMatrix< T, x, y> :: operator * ( CMatrix < T, y, z> m)
{
CMatrix < T, x, z> r;
// blah blah blah
return r;
}
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.
Last edited by carnaz; April 2nd, 2003 at 03:15 AM.
-
April 2nd, 2003, 04:09 AM
#2
Don't make the matrix sizes template parameters.
-
April 2nd, 2003, 07:37 AM
#3
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
-
April 2nd, 2003, 08:26 AM
#4
The following compiles:
Code:
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.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
April 2nd, 2003, 08:34 AM
#5
I just tried the following (with the above definitions) in VC++6.0
Code:
Matrix<int, 2, 3> mat23;
Matrix<int, 3, 4> mat34;
Matrix<int, 2, 4> mat24;
mat24 = mat23*mat34;
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>:  perator*(const Matrix<T, y, z> &)"
(declared at line 7)
Matrix<T, x, z> Matrix<T, x, y>:  perator*(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.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
April 2nd, 2003, 08:40 AM
#6
The answer is:
Code:
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.
Last edited by Graham; April 2nd, 2003 at 08:42 AM.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
April 2nd, 2003, 10:55 AM
#7
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.
*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/
"It's hard to believe in something you don't understand." -- the sidhi X-files episode
galathaea: prankster, fablist, magician, liar
-
April 2nd, 2003, 01:58 PM
#8
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:
Code:
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).
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|