38

I have just come across this keyword C++ Mixin-Style, do anyone know what this is?

In this post, is has been answered as a design pattern. Is it the same design pattern as described in this document?

Community
  • 1
  • 1
Bitmap
  • 12,402
  • 16
  • 64
  • 91
  • 2
    Have you read http://www.cs.umass.edu/~yannis/practical-fmtd.pdf – Seth Carnegie Aug 16 '11 at 21:35
  • Yeah, I know what this is :) You refered short and good description. Another good description can be found in "Modern C++ desing" from Andrei Alexandrescu. Can you please elaborate your question? – Andriy Tylychko Aug 16 '11 at 21:51

5 Answers5

24

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.

BlueRaja - Danny Pflughoeft
  • 84,206
  • 33
  • 197
  • 283
16

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)

daxim
  • 39,270
  • 4
  • 65
  • 132
tp1
  • 1,197
  • 10
  • 17
  • isn't the first mixin case that you have the same (or similar to the traits)? – dragonxlwang Jan 08 '21 at 19:22
  • The first example is really useful, as you don't have to change any class in order to get extend its behavior. But I am still puzzled over how can the client side is supposed to benefit from a mixin: May the client require that you pass a `Mixin2 const&` to a function? Can clients only be function templates, where the (mixin) parameter is passed as a template parameter? Can you provide a couple of example where a Mixin2> may be exploited ? – Grim Fandango Jun 01 '22 at 07:52
  • I also thank you for mentioning that "CRTP is different technique". – Grim Fandango Jun 01 '22 at 07:53
8

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++.

Mankarse
  • 39,818
  • 11
  • 97
  • 141
5

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).

PlasmaHH
  • 15,673
  • 5
  • 44
  • 57
  • I disagree. The point of "Mixins" is to *avoid* Multiple Inheritance (either by language design/restrictions as in Scala, Ruby, and Squeak or through CRTP in C++) -- to solve 90% the problems of sharing implementation for only 10% of the complications (of MI). I made up those numbers, btw :-) –  Aug 16 '11 at 21:43
  • 4
    @pst, did you know that 84% of statistics are made up on the spot? Every time I've seen a mixin, CRTP or not, it was using multiple inheritance. I think that's the meaning of *mix*, it's meant to be mixed with other base classes. – Mark Ransom Aug 16 '11 at 21:49
  • 2
    According to Jim Coplien in his *Advanced C++* mix-ins were a form of dynamic multiple inheritance present in the Flavors language, where you could combine a number of base classes at runtime to create a specific derived class. – Nicola Musatti Aug 16 '11 at 22:05
  • @pst: the unique featuer of a mixin is that it is meant to be used in inheritance, but not for being instantiated alone. Avoiding MI due to inheritance chanining is *one* way to design mixins, but their key point is still to reuse code, not to prevent MI. – PlasmaHH Aug 17 '11 at 09:03
3

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.

Jesse Pepper
  • 3,225
  • 28
  • 48
  • 1
    Mixins are a design inversion of CRTP. The two are distinct, and mixins are not implemented using CRTP at all. – user3814483 Jul 02 '21 at 15:48