Understanding Templates: why can't anybody do it?

H. S. Teoh hsteoh at quickfur.ath.cx
Sat Mar 17 17:46:40 PDT 2012


On Sat, Mar 17, 2012 at 08:37:35PM -0400, Nick Sabalausky wrote:
> "H. S. Teoh" <hsteoh at quickfur.ath.cx> wrote in message 
> news:mailman.834.1332023905.4860.digitalmars-d at puremagic.com...
> >
> > (Not to mention, D templates can do some stuff that no OO can hope
> > to attain. But it goes both ways. Templates can't do runtime
> > polymorphism either.)
> >
> 
> Combined with compile-time reflection, I'm sure they could be used to
> create a runtime polymorphism tool, even multiple dispatch. Like I
> mentioned recently in a seperate thread, you'd just need to use
> templates/ctfe to automate/genericize this:
> 
> void singleDispatch(Object o)
> {
>     if(auto derived = cast(DerivedClassA)o)
>         derived.foo();
>     else if(auto derived = cast(DerivedClassB)o)
>         derived.foo();
>     else if(auto derived = cast(DerivedClassC)o)
>         derived.foo();
>     //etc...
> }
> 
> Although I guess *technically* you're still relying on OO polymorphism
> (in the form of downcasting) to achieve this. OTOH, you could still
> base it on Variant or Algebraic instead of (or in addition to) Object.
> Then you'd have built runtime polymorphism out of templates (among
> other things) without relying on OO.
[...]

But now you're just reimplementing OO in terms of templates. :-) You can
do OO in C, too. (In fact, at work some of the C code I deal with are
approximations to OO.) That has nothing to do with templates themselves
per se.


T

-- 
He who does not appreciate the beauty of language is not worthy to bemoan its flaws.


More information about the Digitalmars-d mailing list