Jump to page: 1 2
Thread overview
CRTP in D?
Aug 19, 2009
bearophile
Aug 19, 2009
grauzone
Aug 19, 2009
bearophile
Aug 20, 2009
Kagamin
Aug 19, 2009
downs
Aug 19, 2009
div0
Aug 19, 2009
Bill Baxter
Aug 19, 2009
div0
Aug 19, 2009
bearophile
Aug 19, 2009
div0
Aug 20, 2009
John C
Aug 20, 2009
div0
Aug 20, 2009
Bill Baxter
Aug 20, 2009
div0
Aug 19, 2009
Saaa
Aug 19, 2009
Bill Baxter
Aug 19, 2009
Saaa
August 19, 2009
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
August 19, 2009
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

Why don't you just go and try?
If you hit forward referencing errors when using structs, try classes with final methods.

> Bye,
> bearophile
August 19, 2009
grauzone:
>Why don't you just go and try?<

Mostly because C++ isn't one of the languages I know. I don't fully understand that code.

Bye,
bearophile
August 19, 2009
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" :)
August 19, 2009
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.
And yes CRTP does work in D.

- --
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
August 19, 2009
On Wed, Aug 19, 2009 at 10:32 AM, div0<div0@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
August 19, 2009
Bill Baxter wrote:
> On Wed, Aug 19, 2009 at 10:32 AM, div0<div0@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();
>    }
> }

Yes true, but there are subtle differences. I guess my no was a little over egging the differences on reflection.

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

CRTP is often used that way, but it's not the only use of it.

It's also used as a mechanism to create a meta interface which deriving classes need to implement. This is used quite a lot in template meta programming in c++. (and that's where you often see the multiple inheritance from policy classes you mentioned).

Mixins do pretty much same thing but in reverse, mixins pull stuff right into the class, and with a mixin it's the class which is imposing the meta interface on the mixin.

As you pointed out though, CRTP nukes your inheritance slot in D, so you'd generally prefer mixins in D. CRTP isn't going to give you any advantage that I can see, you just have to do your design the other way round than you would in c++.

- --
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
August 19, 2009
Bill Baxter wrote:
> On Wed, Aug 19, 2009 at 10:32 AM, div0<div0@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();
>    }
> }
> 
While we're on the subject, is it possible to mixin in a tuple? Doesn't seem like you can...

class C(M...) {
 mixin M;
}

Doesn't work.

- --
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk
August 19, 2009
div0:

> Mixins do pretty much same thing but in reverse, mixins pull stuff right into the class, and with a mixin it's the class which is imposing the meta interface on the mixin.
> 
> As you pointed out though, CRTP nukes your inheritance slot in D, so you'd generally prefer mixins in D. CRTP isn't going to give you any advantage that I can see, you just have to do your design the other way round than you would in c++.

Thank you to all the people that have answered me.
Mixins have an advantage: I have understood them in minutes and then I have used them. While I haven't undertood CRTP that quickly :-)

Bye,
bearophile
August 19, 2009
> 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();
>   }
> }
>

where can I read about class parameters?


« First   ‹ Prev
1 2