CRTP in D?

Bill Baxter wbaxter at gmail.com
Wed Aug 19 11:00:00 PDT 2009


On Wed, Aug 19, 2009 at 10:32 AM, div0<div0 at users.sourceforge.net> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> downs wrote:
>> bearophile wrote:
>>> I don't know much C++. Can CRTP be used in D1 too, to improve the performance of some D1 code?
>>>
>>> http://en.wikipedia.org/wiki/Curiously_Recurring_Template_Pattern
>>>
>>> Bye,
>>> bearophile
>>
>> We have this, except we call it "template mixin" :)
>
> No, template mixins are not CRTP.

Mixins can be used to do a lot (most? all?) of things CRTP is used for:

class Class(alias MixMe)
{
   mixin MixMe impl;
   ...
   void doSomething {
         impl.doIt();
   }
}


> And yes CRTP does work in D.

That's fine if you don't need to use the one inheritance slot for
something else.  I also seem to recall for things like policy based
design, you end up doing CRTP inheritance from several different
policy classes:

class Derived : Policy1<Derived>, Policy2<Derived>

So I think DK is right -- more often than not CRTP is used as a
substitute for lack of actual mixin support in C++.

--bb


More information about the Digitalmars-d-learn mailing list