D - D templates are officially awesome
- Andy Friesen (8/8) Jan 25 2004 Test code attached. The output is:
- Sean L. Palmer (11/53) Jan 25 2004 Gat damn dude!
- roel.mathys yucom.be (4/6) Jan 25 2004 This is getting better and better.
- Matthias Becker (2/4) Jan 26 2004 I looked for it, too, but it seems like it's still missing. But it was
- Achilleas Margaritis (9/51) Jan 26 2004 From what I understand, you make a class inherit indirectly from itself,
- Andy Friesen (22/27) Jan 26 2004 The example I posted is silly, I know. Here's another example that's
- Matthew (8/34) Jan 27 2004 inherits
- Sean L. Palmer (5/10) Jan 26 2004 You should not assume people are so unintelligent, incapable of learning...
- Andy Friesen (6/9) Jan 26 2004 To be fair, A, B, C, and D are pretty cryptic class names. I'd be
- Sean L. Palmer (5/10) Jan 27 2004 I'll bite:
- Matthew (4/13) Jan 27 2004 You've got an extra semi in there, dude!
Test code attached. The output is: C A C B C D A D C B C D C D B We don't need no stinking multiple inheritance! -- andy
Jan 25 2004
Gat damn dude! That is good! I didn't realize you could define class templates like that... when did that change? I thought use of the template keyword was still required! Probably when the ! system went live. I should pay attention... Good stuff Walter! Sean "Andy Friesen" <andy ikagames.com> wrote in message news:bv1ljc$14em$1 digitaldaemon.com...Test code attached. The output is: C A C B C D A D C B C D C D B We don't need no stinking multiple inheritance! -- andy---------------------------------------------------------------------------- ----interface I { void foo();} class A : I { void foo() { printf("A\n"); } } class B : I { void foo() { printf("B\n"); } } class C(T) : T { void foo() { printf("C "); super.foo(); } } class D(T) : T { void foo() { printf("D "); super.foo(); } } alias C!(A) CA; alias C!(B) CB; alias C!(D!(A)) CDA; alias D!(C!(B)) DCB; alias C!(D!(C!(D!(B)))) CDCDB; void main() { I ca = new CA(); I cb = new CB(); ca.foo(); cb.foo(); I cda = new CDA(); I dcb = new DCB(); cda.foo(); dcb.foo(); I cdcdb = new CDCDB(); cdcdb.foo(); }
Jan 25 2004
Gat damn dude! That is good!This is getting better and better. I can't find this syntax documented. bye, roel
Jan 25 2004
This is getting better and better. I can't find this syntax documented.I looked for it, too, but it seems like it's still missing. But it was introduced three versions back :O
Jan 26 2004
From what I understand, you make a class inherit indirectly from itself, using templates ? for example class C inherits from class D which inherits from class C which inherits from D which inherits from B (in the case of CDCDB) ? why do you consider this an important feature ? All will do is confuse the other programmers of your team. "Andy Friesen" <andy ikagames.com> wrote in message news:bv1ljc$14em$1 digitaldaemon.com...Test code attached. The output is: C A C B C D A D C B C D C D B We don't need no stinking multiple inheritance! -- andy---------------------------------------------------------------------------- ----interface I { void foo();} class A : I { void foo() { printf("A\n"); } } class B : I { void foo() { printf("B\n"); } } class C(T) : T { void foo() { printf("C "); super.foo(); } } class D(T) : T { void foo() { printf("D "); super.foo(); } } alias C!(A) CA; alias C!(B) CB; alias C!(D!(A)) CDA; alias D!(C!(B)) DCB; alias C!(D!(C!(D!(B)))) CDCDB; void main() { I ca = new CA(); I cb = new CB(); ca.foo(); cb.foo(); I cda = new CDA(); I dcb = new DCB(); cda.foo(); dcb.foo(); I cdcdb = new CDCDB(); cdcdb.foo(); }
Jan 26 2004
Achilleas Margaritis wrote:From what I understand, you make a class inherit indirectly from itself, using templates ? for example class C inherits from class D which inherits from class C which inherits from D which inherits from B (in the case of CDCDB) ? why do you consider this an important feature ? All will do is confuse the other programmers of your team.The example I posted is silly, I know. Here's another example that's only a little less so: alias HttpStream!( // pull from http ZlibStream!( // decompress as we go TextReader!( // read line by line (or whatever) ExtraErrorChecking!( // throw IOError if anything so much as twitches the wrong way Object // have to end the expression somehow :) ))))) Something_That_Reads_Compressed_Text_From_a_Web_Server; It's weird, but it achieves the same effect as mixins or policies without much fuss. (casting to the superclasses is effectively useless, as each template instance is its own type; to alleviate this, you can make mixin classes inherit an interface as well as its template argument) By having each class override methods and chain them to the superclass, you could also forseeably do some neat things along the lines of the GoF decorator pattern. Anyway, I just thought it was a cool idiom. Maybe someone else will think up a better place to use it. :) -- andy
Jan 26 2004
"Andy Friesen" <andy ikagames.com> wrote in message news:bv47nq$2cnm$1 digitaldaemon.com...Achilleas Margaritis wrote:inheritsFrom what I understand, you make a class inherit indirectly from itself, using templates ? for example class C inherits from class D whichThis is known as a bolt-in, and features large in several template libs, including ATL. Speaking modestly: this is chapter 22 of "Imperfect C++". :) But take heart Dear frienDs, these issue will also feature large in the D book. Reviewer volunteers??from class C which inherits from D which inherits from B (in the case of CDCDB) ? why do you consider this an important feature ? All will do is confuse the other programmers of your team.The example I posted is silly, I know. Here's another example that's only a little less so: alias HttpStream!( // pull from http ZlibStream!( // decompress as we go TextReader!( // read line by line (or whatever) ExtraErrorChecking!( // throw IOError if anything so much as twitches the wrong way Object // have to end the expression somehow :) ))))) Something_That_Reads_Compressed_Text_From_a_Web_Server; It's weird, but it achieves the same effect as mixins or policies without much fuss. (casting to the superclasses is effectively useless, as each template instance is its own type; to alleviate this, you can make mixin classes inherit an interface as well as its template argument) By having each class override methods and chain them to the superclass, you could also forseeably do some neat things along the lines of the GoF decorator pattern. Anyway, I just thought it was a cool idiom. Maybe someone else will think up a better place to use it. :)
Jan 27 2004
You should not assume people are so unintelligent, incapable of learning. If they are, why the $%*& did you hire them in the first place? Sean "Achilleas Margaritis" <axilmar b-online.gr> wrote in message news:bv3uj0$1slt$1 digitaldaemon.com...From what I understand, you make a class inherit indirectly from itself, using templates ? for example class C inherits from class D which inherits from class C which inherits from D which inherits from B (in the case of CDCDB) ? why do you consider this an important feature ? All will do is confuse the other programmers of your team.
Jan 26 2004
Sean L. Palmer wrote:You should not assume people are so unintelligent, incapable of learning. If they are, why the $%*& did you hire them in the first place?To be fair, A, B, C, and D are pretty cryptic class names. I'd be confused too. :) "Real Programmers use C++ because they can do for (int i=a->b++;++c<=3+(d|3;);d*=++a-c%d)" -- andy
Jan 26 2004
I'll bite: that won't compile because (d|3;) is a syntax error. ;) Sean "Andy Friesen" <andy ikagames.com> wrote in message news:bv4dvr$2mt9$1 digitaldaemon.com...To be fair, A, B, C, and D are pretty cryptic class names. I'd be confused too. :) "Real Programmers use C++ because they can do for (int i=a->b++;++c<=3+(d|3;);d*=++a-c%d)" -- andy
Jan 27 2004
You've got an extra semi in there, dude! "Andy Friesen" <andy ikagames.com> wrote in message news:bv4dvr$2mt9$1 digitaldaemon.com...Sean L. Palmer wrote:learning.You should not assume people are so unintelligent, incapable ofIf they are, why the $%*& did you hire them in the first place?To be fair, A, B, C, and D are pretty cryptic class names. I'd be confused too. :) "Real Programmers use C++ because they can do for (int i=a->b++;++c<=3+(d|3;);d*=++a-c%d)" -- andy
Jan 27 2004