In my current project, I need to be able to provide template-based multiple inheritance (Mixin pattern) and have two classes (with mirroring multiple-inheritance trees) that can interact together (i.e. one uses methods from the other one at the same inheritance level).
Long story short, I cannot seem to find an elegant way to build this. Below is a reduced testcase (you can run and edit it directly here).
Is there a pattern or trick that would let me have something similar to the commented line while keeping any subclasses relatively cruft-free?
Obviously, the nested Thing
class would need to inherit from another class providing the necessary interface methods. However any attempt at fixing the inheritance problem (CRTP...) always seem to lead me to recursive inheritance or incomplete base type issues...
class Base {
public:
class Thing {
public:
Thing(Base& b) : _b(b) {};
Thing& operator+= (const Thing p) { _b.add(*this, p); return *this; };
int k;
protected:
Base& _b;
};
void add(Thing &d, const Thing s) { d.k += s.k; }
};
template <class... Interfaces>
class Extensible : virtual public Base, virtual public Interfaces... {
class Thing : virtual public Base::Thing, virtual public Interfaces::Thing... {
};
};
class SomeInterface : Base {
void multiply(Thing &d, const Thing s) { d.k *= s.k; }
class Thing : public Base::Thing {
Thing& operator*= (const Thing p) {
//_b.multiply(*this, p); return *this; // <-- won't work of course
};
};
};
int main() {
Extensible<SomeInterface> a;
return 0;
}