Operators code duplication #110
buschbapti
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Discussions in PR #108 highlighted the fact that operators for
CartesianState
andJointState
consist in a lot of code duplication in the derived states. The main issue is that if one operator is redefined in a derived class, then all the same type operators (e.g. all theoperator*
functions, irrelevant of their signature) have to be redefined as well.Now there are some ways to try to correct that than simply redefining all the operators in the derived classes. One idea proposed by @eeberhard was to use the keyword
using
on the base class operators (effectively addingusing BaseClass::operatorX
in the header files of the derived class). This does solve the issue as redefining operators is then not needed anymore. However, it does mean that the return type of the operator is of typeBaseClass
and notDerivedClass
. In a sense that is not so much of an issue, as copy constructors and assignment operators are defined and now fully reliable after PR #100 & #101 so we can always store the result of an operator in one of the derived class. But, it means that inline equations, that were relying on the operation returning a derived class are not valid anymore.Another option that I tried to implement is to use the CRTP design pattern https://www.fluentcpp.com/2020/05/22/how-to-assign-derived-classes-in-cpp/, i.e. using an intermediary template class between the base and the derived classes that have access to both types. This seems like an elegant solution but there are a few hiccups that complicates its implementation. So far my testing have been infructuous to come up with a working implementation. The main drawback of the solution shown in the above link seems that the operator needs to be pure virtual in the base class. In our case we do not want that as we actually need the operations to be valid for the base class as well. Maybe there is a workaround and this is not actually needed. However, as the derived class now inherit from this template intermediary and not directly from the base class, I haven't been able to find a proper way to use the base class constructor in the derived classes. Ideally, we would like this template class to be the same for both
JointState
andCartesianState
as the operators behave similarly.I will try to dive a bit further in this CRTP design as I think this is the solution that would be the more elegant and lead to the removal of most of the code duplication, while maintaining similar functionalities to what we have at the moment.
Beta Was this translation helpful? Give feedback.
All reactions