On the richness of C++

Sean Kelly sean at invisibleduck.org
Wed Apr 16 11:31:17 PDT 2008


== Quote from Walter Bright (newshound1 at digitalmars.com)'s article
> Sean Kelly wrote:
> > Sure, but templates don't suit every situation.  It's not uncommon for
> > APIs to have to overload on accepting a function and a delegate for
> > flexibility.
> The only way to merge function pointers with delegates is to have the
> compiler generate trampolines. This isn't very runtime efficient - the
> template approach will be faster.

Certainly.  Templates are always faster.  But they don't always suit.  To
choose the simplest example, let's say that I want to ship a library with
no source code exposed.  Templates are obviously out.  Now, to support
callable parameters I must either overload routines for both function and
delegate simply to account for pointers to global vs. non-global callable
routines, or I need to use a wrapper object to pass instead.  Either way,
the same efficiency is lost, and I'm stuck doing a bunch of wrapping or
conversion in library code as well.  Now perhaps the library is indeed the
best place for this, but either way I disagree that "just use templates
because they're faster" is an acceptable solution for every situation.  The
80-20 rule dictates that generalized arguments about performance are
baseless anyway because 80% of the time the performance difference
isn't even noticeable.  Rather, I would argue that in such instances
elegance is of utmost importance because of its effect on code
maintainability.  The great thing about D is that it often manages to
achieve top-tier performance in an eminently elegant manner.


Sean



More information about the Digitalmars-d mailing list