Nobody understands templates?

Meta jared771 at gmail.com
Fri Feb 28 11:56:39 PST 2014


On Friday, 28 February 2014 at 18:42:57 UTC, Steve Teale wrote:
> All the D aficionados seem to wet their pants over
> meta-programming, but I struggle to find a place to use it.
>
> IIRC, I used it in a couple of places when I was trying to write
> library stuff for MySQL, but in my current project, I use it 
> only
> once. That's when I want to stuff something onto my undo stack.
>
> For that I have two template functions - push(T)(MybaseClass* p,
> T t, int ID), and pushC, which is just the same except that it
> checks the top of the stack to see if the ID there is the same 
> as
> what it is wanting to push.
>
> This has served me very reliably, but I struggle to find other
> places in the whole application where I would benefit from
> templates.
>
> Is this typical - libraries use templates, applications don't, 
> or
> am I just being unimaginative?
>
> Steve

It really depends on what you're writing. If you look at Phobos, 
which is meant to be as general as possible, I'd estimate that 
about 70% of it is templated, possibly more. I find that general 
utilities (such as your push function) are pretty much always 
template, while most program logic isn't. If you find yourself 
writing mainly program logic with very little need for utility 
functions, then it shouldn't be surprising that you are not using 
templates that much. The one exception is when doing stuff that 
requires a lot of boilerplate; mixins and template mixins are 
extremely useful for that.


More information about the Digitalmars-d-learn mailing list