www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Curiously Recurring Template pattern (CRTP)

reply "Walter" <newshound digitalmars.com> writes:
This was originally on comp.lang.c++.moderated:

"Robert Kindred" <RKindred SwRI.edu> wrote in message
news:116a3fter9qeq07 corp.supernews.com...
 I know of no other language [besides C++] capable of the Curiously
 Recurring Template Pattern (Leda does not count).

Here's the wiki entry on the CRTP: http://c2.com/cgi/wiki?CuriouslyRecurringTemplate and the D Programming Language implementation of it: class ArithmeticType(T) { T opAdd(T other) { T result = new T(this); result += other; return result; } } class Quaternion: ArithmeticType!(Quaternion) { this() { } this(ArithmeticType!(Quaternion) t) { } Quaternion opAddAssign(Quaternion other) { // etc. } } So now there are two languages that can do the CRTP <g>.
Apr 23 2005
parent "TechnoZeus" <TechnoZeus PeoplePC.com> writes:
"Walter" <newshound digitalmars.com> wrote in message
news:d4f1o0$2s4n$1 digitaldaemon.com...
 This was originally on comp.lang.c++.moderated:

 http://c2.com/cgi/wiki?CuriouslyRecurringTemplate

 and the D Programming Language implementation of it:

 class ArithmeticType(T)
 {
     T opAdd(T other)
     {
         T result = new T(this);
         result += other;
         return result;
     }
 }

 class Quaternion: ArithmeticType!(Quaternion)
 {
     this() { }
     this(ArithmeticType!(Quaternion) t) { }

     Quaternion opAddAssign(Quaternion other)
     {
          // etc.
     }
 }

 So now there are two languages that can do the CRTP <g>.

Cool! Thanks for pointing this out. :)
Apr 24 2005