CRTP in D?
div0
div0 at users.sourceforge.net
Wed Aug 19 12:45:26 PDT 2009
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Bill Baxter wrote:
> 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();
> }
> }
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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iD8DBQFKjFZWT9LetA9XoXwRAu7jAKDN8lOVjr1JW9F+HpWvu/rWScQIqwCguYED
fVaoenX/OgKfsmXR4etVSas=
=9QPj
-----END PGP SIGNATURE-----
More information about the Digitalmars-d-learn
mailing list