What is C++ Mixin-Style?

Solution 1:

Mixins are a concept from Lisp. A good explanation from Dr. Dobbs:

A mixin is a fragment of a class in the sense that it is intended to be composed with other classes or mixins.
[...]
The difference between a regular, stand-alone class (such as Person) and a mixin is that a mixin models some small functionality slice (for example, printing or displaying) and is not intended for standalone use. Rather, it is supposed to be composed with some other class needing this functionality (Person, for instance).

So, the point of a mixin is to allow something like multiple-inheritance, without all the Bad Things™ that usually come along with multiple inheritance.

This can be a bit confusing, however, because C++ does not natively support mixins; in order to "do" mixins in C++, you have to use multiple-inheritance! What this ends up meaning in practice is that you still use multiple-inheritence, but you artifically limit what you allow yourself to use it for.

See the article above for an actual mixin implementation.

Solution 2:

If I remember this correctly, there are at least two ways to create mixins in C++. This comes from some very old (1995) tutorial I've seen (but it's almost now completely disappeared from the internet).

First,

class MixinBase {
public :
    void f() {};
};

template<class T>
class Mixin : public T {
public:
    void f() {
        T::f();
        T::f();
    }
};

template<class T>
class Mixin2 : public T {
public :
    void g() {
        T::f();
        T::f();
    }
};

int main() {
    Mixin2<Mixin<MixinBase>> mix;
    mix.g();
}

Or another way uses virtual inheritance, and sibling calls:

class Base {
public :
    virtual void f() = 0;
};

class D1 : public virtual Base {
public :
    void g() {
        f();
    }
};

class D2 : public virtual Base {
public :
    void f() {
    }
};

class D : public D1, public D2 {
};

int main() {
    D d;
    d.g();
}

Now these both versions implement mixins, because Mixin and Mixin2 are independent classes, but they can still communicate. And you can create software from this kind of modules and then later just bind those modules to one big software. Same happens between D1 and D2 in the virtual inheritance. Important thing to notice that in mixin design the different modules live inside the same c++ object. (oh and CRTP is different technique)

Solution 3:

A Mixin is a class (or other grouping of code) that is intended to be reused through direct inclusion in another piece of code. Think of it as inheritance without sub-type polymorphism. The CRTP is a way of approximating Mixins in C++.

Solution 4:

Usually mixins are referred to as small classes (often templated or crtp based) that you derive from to "mix in" some functionality; usually via multiple inheritance and in policy based designs (also see "Modern C++ Design" by Alexandrescu).

Solution 5:

Mixins in C++ are expressed using the Curiously Recurring Template Pattern (CRTP). This post is an excellent breakdown of what they provide over other reuse techniques... compile-time polymorphism.