digitalmars.D - An Impressive Feature Without Doc
- Wei Li (11/11) Nov 13 2007 Just a moment ago, I met a cool feature but where is the specs for it?
Just a moment ago, I met a cool feature but where is the specs for it? template Floats(T) { static if(is(T : real)) { alias T Floats; } } T min(T=Floats)(T x, T y) // Why we can write T=Floats instead of T=Floats!(T) ? { return x < y ? x : y; } Regards
Nov 13 2007
Wei Li Wrote:Just a moment ago, I met a cool feature but where is the specs for it? template Floats(T) { static if(is(T : real)) { alias T Floats; } } T min(T=Floats)(T x, T y) // Why we can write T=Floats instead of T=Floats!(T) ? { return x < y ? x : y; } RegardsSorry, my fault. I'm confused it with ':'. Regards
Nov 13 2007
Wei Li wrote:Just a moment ago, I met a cool feature but where is the specs for it? template Floats(T) { static if(is(T : real)) { alias T Floats; } } T min(T=Floats)(T x, T y) // Why we can write T=Floats instead of T=Floats!(T) ? { return x < y ? x : y; } RegardsThis is one of D's template pitfalls; since min is never instantiated, it never actually checks if Floats is a valid thing to instantiate min with. (it's not; a template is not a type). You can see this by writingvoid main() { auto fp=&min!(); }which will make the compiler notice the error and barf. Thanks to KMD for pointing this out.
Nov 14 2007
downs Wrote:Wei Li wrote:I got it, thanks for your explain. Regards,Just a moment ago, I met a cool feature but where is the specs for it? template Floats(T) { static if(is(T : real)) { alias T Floats; } } T min(T=Floats)(T x, T y) // Why we can write T=Floats instead of T=Floats!(T) ? { return x < y ? x : y; } RegardsThis is one of D's template pitfalls; since min is never instantiated, it never actually checks if Floats is a valid thing to instantiate min with. (it's not; a template is not a type). You can see this by writingvoid main() { auto fp=&min!(); }which will make the compiler notice the error and barf. Thanks to KMD for pointing this out.
Nov 14 2007