![]() Registered Member ![]()
|
I am facing the problem that I have a virtual function that needs to be able to take both MatrixXf and Map<MatrixXf> (the latter because there's some memory sharing going on). MatrixBase would be the right base type (I think) , but that would require templates, and you can't use templates with virtual functions in C++.
The closest related thread I found is this thread, but Refs seem to be tied to a certain derived class, so I don't think they help in my case. Do you have any suggestions on on how to go about this? Thanks. |
![]() Registered Member ![]()
|
Wow!, very interesting topic. I never had that problem but I can foresee having it in a not very far future. Your question took me into doing some reading about this and I found out some very interesting articles about type erasure that I think might help. Take a look and I hope it helps. Good luck!
Answer by David Rodriguez in a stack overflow post: http://stackoverflow.com/a/7968213/1889975 An article on type erasure: http://www.artima.com/cppsource/type_erasure.html |
![]() Registered Member ![]()
|
I've researched various suggestions online regarding a workaround for the virtual template function in C++, and found these:
- Template the whole class instead of just the function. Maybe I'm missing something, but that won't work, because it only kicks the can further down the road. - Use a Visitor pattern. That's a pretty hefty API change, so I'm not sure I want to go down that path. In the end, for now I will drop support for Map<Matrix> and just use Matrix, thus not needing template functions. |
![]() Moderator ![]()
|
You can use Eigen's Ref<> class for your purpose:
http://eigen.tuxfamily.org/dox-devel/cl ... _1Ref.html A Ref<Matrix> can accept either a Matrix, a Map<Matrix> and even a Block<Matrix> without needing templates. |
Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]