Thread overview | |||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 22, 2004 DMD compiler does like template recursion | ||||
---|---|---|---|---|
| ||||
that is version .79 does bye, roel ----------------------- template factor(int i) { const int value = i * factor!(i-1).value; } template factor(int i : 0) { const int value = 1; } int main() { int f = factor!(5).value; printf("%d\n",f); return 0; } |
January 22, 2004 Re: DMD compiler does like template recursion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Roel Mathys | In article <bup7bl$2t43$1@digitaldaemon.com>, Roel Mathys says... > >that is version .79 does > >bye, >roel >----------------------- > >template factor(int i) >{ > const int value = i * factor!(i-1).value; >} > >template factor(int i : 0) >{ > const int value = 1; >} > >int main() >{ > int f = factor!(5).value; > printf("%d\n",f); > > return 0; >} Somebody better explain this to me! I would've thought that functions are chose by signature, and not by value?? |
January 23, 2004 Re: DMD compiler does like template recursion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Roel Mathys | Can someone explain to me the benefits of this ( meta programming ? ) besides finding 5 factorial ? The only artical I read on it using C++ did the same thing , what other uses does it have ? Thanks, C "Roel Mathys" <roel.mathys@yucom.be> wrote in message news:bup7bl$2t43$1@digitaldaemon.com... > that is version .79 does > > bye, > roel > ----------------------- > > template factor(int i) > { > const int value = i * factor!(i-1).value; > } > > template factor(int i : 0) > { > const int value = 1; > } > > int main() > { > int f = factor!(5).value; > printf("%d\n",f); > > return 0; > } |
January 23, 2004 Re: DMD compiler does like template recursion | ||||
---|---|---|---|---|
| ||||
Posted in reply to C | That realizes: const int Size = factor!(5).value; int[Size] n; "C" <dont@respond.com> wrote in message news:bupp7r$pa3$1@digitaldaemon.com... > Can someone explain to me the benefits of this ( meta programming ? ) besides finding 5 factorial ? The only artical I read on it using C++ did the same thing , what other uses does it have ? > > Thanks, > C > "Roel Mathys" <roel.mathys@yucom.be> wrote in message > news:bup7bl$2t43$1@digitaldaemon.com... > > that is version .79 does > > > > bye, > > roel > > ----------------------- > > > > template factor(int i) > > { > > const int value = i * factor!(i-1).value; > > } > > > > template factor(int i : 0) > > { > > const int value = 1; > > } > > > > int main() > > { > > int f = factor!(5).value; > > printf("%d\n",f); > > > > return 0; > > } > > |
January 23, 2004 Re: DMD compiler does like template recursion | ||||
---|---|---|---|---|
| ||||
Posted in reply to C | C wrote:
> Can someone explain to me the benefits of this ( meta programming ? )
> besides finding 5 factorial ? The only artical I read on it using C++ did
> the same thing , what other uses does it have ?
I've heard of it being used to get the compiler to generate lookup tables. It also has uses in places where you want to loop some number of times that is known at compile time, but don't want the runtime overhead of a loop. (matrix multiplication, for instance, can be done with template metaprogramming, if the matrix dimensions are template arguments)
This is definitely one of the cooler things about C++ templates, but, come to think of it, it's not useful in a whole lot of cases.
Regardless, it's neat to know that D can swallow it. :)
-- andy
|
January 23, 2004 Re: DMD compiler does like template recursion | ||||
---|---|---|---|---|
| ||||
Posted in reply to C | A question with an enormous answer. I can't think of a smart answer right now, but I can assure you that DTL will make intelligent use of meta programming techniques. Promise. :) "C" <dont@respond.com> wrote in message news:bupp7r$pa3$1@digitaldaemon.com... > Can someone explain to me the benefits of this ( meta programming ? ) besides finding 5 factorial ? The only artical I read on it using C++ did the same thing , what other uses does it have ? > > Thanks, > C > "Roel Mathys" <roel.mathys@yucom.be> wrote in message > news:bup7bl$2t43$1@digitaldaemon.com... > > that is version .79 does > > > > bye, > > roel > > ----------------------- > > > > template factor(int i) > > { > > const int value = i * factor!(i-1).value; > > } > > > > template factor(int i : 0) > > { > > const int value = 1; > > } > > > > int main() > > { > > int f = factor!(5).value; > > printf("%d\n",f); > > > > return 0; > > } > > |
January 23, 2004 Re: DMD compiler does like template recursion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew | Modern C++ Design: Generic Programming and Design Patterns Applied by Andrei Alexandrescu (Author) C++ Templates: The Complete Guide by David Vandevoorde (Author), Nicolai M. Josuttis (Author) The two books explain in depth what's possible in C++ and how painful it is to implement when you take into consideration requirements like argument dependent lookup (ADL). Many of the techniques described probably apply to D templates as well. I have been working on a C++ source-to-source transformation engine for a global optimizer that is aware of special library semantics. We use EDG (written in part by the first author of the second book mentioned above) as a front-end for parsing and semantic analysis. Depending on how you look at it, this code base is both beautiful and ugly at the same time. You marvel at the beauty because of what it accomplishes and yet you dread its maintenance because the complexity inherent in C++ renders it terribly fragile. Cheers Matthias "Matthew" <matthew.hat@stlsoft.dot.org> wrote in message news:buptji$10ej$1@digitaldaemon.com... > A question with an enormous answer. > > I can't think of a smart answer right now, but I can assure you that DTL will make intelligent use of meta programming techniques. Promise. :) > > "C" <dont@respond.com> wrote in message news:bupp7r$pa3$1@digitaldaemon.com... > > Can someone explain to me the benefits of this ( meta programming ? ) besides finding 5 factorial ? The only artical I read on it using C++ did > > the same thing , what other uses does it have ? > > > > Thanks, > > C > > "Roel Mathys" <roel.mathys@yucom.be> wrote in message > > news:bup7bl$2t43$1@digitaldaemon.com... > > > that is version .79 does > > > > > > bye, > > > roel > > > ----------------------- > > > > > > template factor(int i) > > > { > > > const int value = i * factor!(i-1).value; > > > } > > > > > > template factor(int i : 0) > > > { > > > const int value = 1; > > > } > > > > > > int main() > > > { > > > int f = factor!(5).value; > > > printf("%d\n",f); > > > > > > return 0; > > > } > > > > > > |
January 23, 2004 Re: DMD compiler does like template recursion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthias Spycher | "Matthias Spycher" <matthias@coware.com> wrote in message news:buqdjo$1r0j$1@digitaldaemon.com... > Modern C++ Design: Generic Programming and Design Patterns Applied > by Andrei Alexandrescu (Author) > > C++ Templates: The Complete Guide > by David Vandevoorde (Author), Nicolai M. Josuttis (Author) > > The two books explain in depth what's possible in C++ and how painful it is > to implement when you take into consideration requirements like argument dependent lookup (ADL). Many of the techniques described probably apply to D > templates as well. > > I have been working on a C++ source-to-source transformation engine for a global optimizer that is aware of special library semantics. We use EDG (written in part by the first author of the second book mentioned above) as > a front-end for parsing and semantic analysis. Depending on how you look at > it, this code base is both beautiful and ugly at the same time. You marvel at the beauty because of what it accomplishes and yet you dread its maintenance because the complexity inherent in C++ renders it terribly fragile. Since you're very familiar with this, I'd be interested in your take on whether D's template syntax and semantics improves on it or not, and why. (I have the 2nd book, but not the first.) D templates *will* go under the microscope by the C++ template experts sooner or later, and I'd rather get the critiques of it sooner! |
January 23, 2004 Re: DMD compiler does like template recursion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | The code here under (slightly adapted from C++ to D off course) does not generate a virtual table in C++. These result compile-time dispatching. Is the D compiler capable of optimizing away the virtualness? I don't think so because of the following extract. From the website: D differs from C/C++ in another aspect of casts. Any casting of a class reference to a derived class reference is done with a runtime check to make sure it really is a proper downcast. This means that it is equivalent to the behavior of the dynamic_cast operator in C++. C++ has - beside dynamic_cast - a static_cast operator, which is used in this case. Maybe there are other ways to do it in D, but I am not aware off them at this moment. BTW, this is heavily and heavenly used in WTL, a GUI library for Win32. bye, roel /+ ---------------------------------------------- +/ template H(T) { class Base { void GetInfo() { T* t = cast(T*)(&this); t.Info(); } void Info() { printf("This is a Base instance.\n"); } } } class D1 : H!(D1).Base { // No overrides } class D2 : H!(D1).Base { void Info() { printf("This is a D2 instance.\n"); } } int main() { D1 d1 = new D1; D2 d2 = new D2; d1.GetInfo(); d2.GetInfo(); return 0; } |
January 23, 2004 Re: DMD compiler does like template recursion | ||||
---|---|---|---|---|
| ||||
Posted in reply to Roel Mathys | This is a first shot at implementing typelist-templates in D (I pickpocketed it from C++). I don't know if templates in D are meant to work this way. It does compile, but results are not completely satisfactionary. In the main() body I indicated case1 and case2, changing the sequence of the two printf statements changes the results. Case3 just doesn't work, I guess compiler doesn't detect the compile-time constants. I don't know if I made an error, or did something else wrong. And above that I'm still working myself slowly into D. bye, roel /+ -------------------------------------------------- +/ class StopType { alias StopType Tail; } template isStopType( alias T ) { const int value = 0; } template isStopType( T : StopType ) { const int value = 1; } template L(T1, alias T2) { alias T1 Head; alias T2 Tail; } template L(T) { alias T Head; alias StopType Tail; } template length( alias TL ) { template Help( alias TL2 , int i : 1 ) { const int value = 1; } template Help( alias TL2 , int i : 0 ) { const int value = 1 + Help!( TL2.Tail , isStopType!( TL2.Tail ).value ).value; } const int value = Help!( TL.Tail , isStopType!( TL.Tail ).value ).value; } int main() { alias L!(int) IL; alias L!(double,IL) DIL; alias L!(real,DIL) RDIL; // case 1 printf( "%d\n" , length!( DIL).value ); printf( "%d\n" , length!( IL).value ); // case 2 //printf( "%d\n" , length!( IL).value ); //printf( "%d\n" , length!( DIL).value ); // does not work //printf( "%d\n" , length!(RDIL).value ); return 0; } |
Copyright © 1999-2021 by the D Language Foundation