Nobody understands templates?

Mike Parker aldacron at gmail.com
Fri Feb 28 21:45:55 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?

I use them for custom containers, configurations and the like,
which is the most obvious case -- when multiple types need to be
handled and I don't want to implement the same code for each
type. With my C and Java background (read, limited experience
with templates), it's been hard to recognize other uses. I was
ridiculously happy with myself recently when I realized I could
make my OpenGL vertex buffer code more flexible by using
templates and other compile time constructs to configure
different vertex formats, something that was rather non-obvious
to me before.


More information about the Digitalmars-d-learn mailing list