Base class selection for generic code?
Hi,
I’ve been using your library for years and I’m quite pleased with it from most perspectives. I had one question, however; if I wanted to write software that works with both CustomMatrix, DynamicMatrix, and StaticMatrix, is there a type I can use as a base class so that the same code can work on all of them? I’ve tried inheriting from Matrix, and that hasn’t worked.
I’ve usually ended up just templating by class (template<typename MatType>
), but it would be nice to be stricter about contained types and orientations so that fewer overloads need to be written. (Since different functions need to be written for differing transpose flags.)
This is mostly an issue of user ignorance of how the library works. Either way, I appreciate your time. Thanks!
Comments (3)
-
-
reporter Thank you! That should be sufficient. I very much appreciate the explanation and the example.
-
reporter - changed status to resolved
The answer was promptly provided. I need no further help. This might be useful as an addendum to the documentation.
- Log in to comment
Hi Daniel!
Thanks a lot for the positive feedback, that is very much appreciated
There is indeed a base class that you can use to work with all kinds of dense matrices. In the wiki you’ll find the available base classes:
All of these base classes are based on CRTP, i.e. the first template parameter is the dynamic type of the vector or matrix.
Additionally you’ll find examples for how to use them. For instance, the following example shows the
countZeros()
function, which counts the number of values, which are exactly zero, in a dense, row-major matrix:Note the use of the
operator~()
. By means of this operator you can cast back to the actual type of the dense matrix (e.g.StaticMatrix
orDynamicMatrix
). I hope this helps,Best regards,
Klaus!