An Impressive Feature Without Doc

downs default_357-line at yahoo.de
Wed Nov 14 01:38:40 PST 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;
> }
> 
> Regards

This 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 writing

>
> void main() {
>   auto fp=&min!();
> }
>

which will make the compiler notice the error and barf.
Thanks to KMD for pointing this out.



More information about the Digitalmars-d mailing list