What is C ++ Mixin-Style?

I just met this C++ Mixin-Style keyword, does anyone know what that is?

In this post , the answer was given as a design template. Is this the same design pattern described in this document ?

+35
c ++ mixins
Aug 16 2018-11-21T00:
source share
5 answers

Mixins is a concept from Lisp. Good explanation from Dr. Dobbs :

A mixin is a fragment of a class in the sense that it is intended to be combined with other classes or mixins.
[...]
The difference between a regular, stand-alone class (e.g. Person) and a mixin is that the mixin models a small piece of functionality (e.g. print or display) and is not intended for stand-alone use. Rather, it should be compiled with another class that needs this functionality (e.g. Person).

So, the purpose of the mixin is to allow something like multiple inheritance without all the bad things that are usually accompanied by multiple inheritance.

However, this can be a little confusing because C ++ does not natively support mixins; to make mixins in C ++, you must use multiple inheritance! In practice, this means that you are still using multiple inheritance, but you are artificially restricting what you allow yourself to use it for.

See the article above for the actual implementation of mixin.

+20
Aug 16 2018-11-18T00:
source share

If I remember this correctly, there are at least two ways to create mixins in C ++. This comes from some very old (1995) textbook that I saw (but it almost completely disappeared from the Internet).

Firstly,

 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; dg(); } 

Now both versions implement mixins because Mixin and Mixin2 are independent classes, but they can still communicate. And you can create software from such modules, and then just bind these modules to one large software. The same thing happens between D1 and D2 in virtual inheritance. It is important to note that in the mixin project, different modules live inside the same C ++ object. (oh and CRTP is a different method)

+14
Aug 17 2018-11-11T00:
source share

A mixin is a class (or other grouping of code) that is designed to be reused through direct inclusion in another piece of code. Think of it as inheritance without a subtype of polymorphism. CRTP is a way to approximate Mixins in C ++.

+8
Aug 16 2018-11-21T00:
source share

Mixins in C ++ are expressed using a curiously repeating pattern (CRTP). This post is an excellent description of what they provide compared to other reuse methods ... polymorphism at compile time.

+5
Mar 06 '15 at 2:39
source share

Usually mixins are called small classes (often templated or crtp based), which you get to "mix" some functions; usually through multiple inheritance and policy-based (also see Alexandrescu's "Modern C ++ Design").

+4
Aug 16 2018-11-21T00:
source share



All Articles