I'm writing a hierarchy of classes of C++, let's say A, B inheriting A, C inheriting A, and D inheriting B.
Now, all of these classes must have a method bar() &, whose body is:
{
A::foo();
return *this;
}
It's the exact same code, doing the exact same thing - except for the type of the return value - which returns an lvalue reference to the class' type.
Now, the signature of this method would be different for every class. But - it's essentially the same method. The thing is, the way things stand, I need to replicate the code for it many times. How can I avoid this code duplication?
I was thinking of writing some mixin with CRTP, but when I get into the details it becomes super-ugly.
Note: For the purposes of this example, bar() is only defined for lvalues so as not to get into the question of legitimacy of returning *this from an rvalue.
As Raymond Chen commented, c++23 would have deducing this which allows code like:
But currently, CRTP might help, something like: