The fundamental advantage (inlined templates over macros) is that the compiler knows what's
going on. First, it can provide type checking, which with good design can prevent many usage
errors. Second, it can provide type-based dispatching, enabling different algorithms to be
used for different data structures. The optimal sort for a three-element array is
fundamentally different from that for one of unknown size. The optimal sort for a linked list
is conceptually similar to that for an array, but an entirely different code sequence.
Using template type matching, you can present the same interface for all these cases, and
generate appropriate code for each. Try doing that with C macros, or even Lisp macros.
You might provide macros for all the cases, under different names, but you probably wouldn't,
because you could only use them locally. Templates may be packaged cleanly and safely enough
to be generally usable, justifying more attention to their creation and distribution.
C++ may be faster in other places. In a deep call chain, where each caller must check and
dispatch on the return value of each call, using exceptions well instead can make the code
both faster and much more compact and readable. A taste of the difference may be seen in
numeric codes that rely on checking for Inf and NaN at the end of a computation; imagine
checking for all those conditions after each multiply!
Posted Jun 18, 2008 20:00 UTC (Wed) by flewellyn (subscriber, #5047)
[Link]
Try doing that with C macros, or even Lisp macros.
You wouldn't use macros for that in (Common) Lisp, that's not what they're for. You'd use CLOS generic functions, which would dispatch on the argument types. Then you get a nice, natural, functional interface with multiple dispatch as a free bonus.
Sometimes C++ is faster
Posted Jun 18, 2008 21:00 UTC (Wed) by ncm (subscriber, #165)
[Link]
Sorry, f., we're talking here about speed, hence compile-time dispatching. If you don't care
about runtime cost, all kinds (not to say "sorts") of notational convenience are easy to
support.
Sometimes C++ is faster
Posted Jun 18, 2008 21:29 UTC (Wed) by flewellyn (subscriber, #5047)
[Link]
In CL, if you want compile-time dispatching, you just declare the types of the method
arguments using DECLARE. Then the compiler sees that and "hard-wires" the method dispatch.
Sometimes C++ is faster
Posted Jun 18, 2008 21:51 UTC (Wed) by ncm (subscriber, #165)
[Link]
Does CL support overloading? Or would you need to give the functions DECLAREd with different
argument types different names?
Sometimes C++ is faster
Posted Jun 18, 2008 21:58 UTC (Wed) by flewellyn (subscriber, #5047)
[Link]
You would DECLARE the types of the arguments in the calling function. Then (in some
implementations, anyway) the compiler would see that the arguments to the generic function are
of a particular type, and do the dispatch at compile-time. No changes to the called GF
required.
Sometimes C++ is faster
Posted Jun 18, 2008 21:10 UTC (Wed) by ncm (subscriber, #165)
[Link]
I guess I should mention that this sort of compile-time dispatching is natural, without
macros, in ML variants and in Haskell. Such languages can be good for coding compilers, given
an implementation with a GC that respects cache locality. Recoding Gcc in Eager Haskell, it
might take years before you got anything useful, but you'd learn a lot, and have a better
sense of what the successor to Haskell should look like.
Sometimes C++ is faster
Posted Jun 18, 2008 21:31 UTC (Wed) by flewellyn (subscriber, #5047)
[Link]
Mmh, well, I was hardly recommending that anybody rewrite GCC in Common Lisp. While I'm sure
someone could do it, the result would be a hell of a lot harder to bootstrap. :-)