A Perspective on D from game industry

c0de517e via Digitalmars-d digitalmars-d at puremagic.com
Wed Jun 18 00:58:56 PDT 2014


> Actually I was talking about templates:
>
> 	R find(R,T)(R range, T element)
> 		if (isInputRange!R && is(ElementType!R : T))
> 	{
> 		while (!range.empty)
> 		{
> 			if (range.front == element)
> 				break;
> 			range.popFront();
> 		}
> 		return range;
> 	}

http://en.wikipedia.org/wiki/Parametric_polymorphism

C++ Templates are more general than that, they do express this 
kind of polymorphism but that can be done without "full" 
metaprogramming (turing-complete ability of generating code at 
compile time). A bounded parametric type is better than templates 
(C++ concepts are an attempt to patch templates with bounds)

Also notice that really all this is expressible even in languages 
that have only dynamic polymorphism (subtyping) without 
performance penalties (YES REALLY).

People think that implementing interfaces is for some reason 
inherently slower than templates, the same they believe function 
pointers are slower than functors. It's FALSE. The ONLY reason 
why templates and functors can be faster is because they are 
always inline, the compiler knows exactly what to call without 
indirections. But that's only WORSE than the "indirect" 
alternatives, because interfaces and pointers afford you the 
option not to resolve everything statically, but if you want you 
can always inline everything (put the implementation in headers) 
and the compiler will perfectly know that it can directly call a 
given function without overhead...


More information about the Digitalmars-d mailing list