A $mixin inherrently supports the curiously recurring template pattern, because the final class is passed into the base of the mixin chain.
In the following example, the identifier FinalClass which appears inside the mixin refers to the final class C in the context of the mixin chain of C in which M appears.
$mixin M
{
void f()
{
static_cast<FinalClass*>(this)->g();
}
};
$struct C : mixin [M]
{
void g() { std::cout << "g()\n"; }
};
void Run()
{
C c;
c.f(); // Calls g()
}
$this is a convenient shorthand for static_cast<FinalClass*>(this) inside a mixin, so the above mixin can be written instead as:
$mixin M
{
void f()
{
$this->g();
}
};
$this is also a convenient shorthand for static_cast<const FinalClass*>(this) inside a mixin. For example
$mixin M
{
void f() const
{
$this->g();
}
};