[C++] What is Mixins?

When you have bunch of ideas and concepts need to be modelled in your project, normally you go with inheritance, define common interface classes, but sometimes, they are not related at all or they are orthogonal for the most part.

The idea with mix-ins is to provide a bunch of primitive classes, where each of them models a basic orthogonal concept, and be able to stick them together to compose more complex classes with just the functionality you want. The primitive classes themselves are meant to be used as building blocks. This is extensible since later on you can add other primitive classes to the collection without affecting the existing ones.

For example, you have a Number struct:

Now you want to add a feature like undoable number, the basic idea here is you connect these building blocks together by providing them via the template parameter, without touching Number struct:

Mixins is bad:

  • Hard to read code
  • Hard to debug and maintenance

But Mixins is easy to use, like playing legos, and it is used a lot in Flutter (with Dart), why?